python的特殊方法——魔术方法

前言

init(self[])

[​编辑 call(self [, ...])](#编辑 call(self [, ...]))

[getitem(self, key)](#getitem(self, key))

len(self)

[repr(self) / str(self)](#repr(self) / str(self))

[add(self, other)](#add(self, other))

[radd(self, other)](#radd(self, other))

参考文献


前言

官方定义好的,以两个下划线开头且以两个下划线结尾来命名的方法。在特定情况下,它会被自动调用,不需要我们主动调用该方法。

init(self[])

__init__(self, ...):构造方法,在创建对象时自动调用。用于初始化对象的属性。

python 复制代码
class Person:
    def __init__(self, name):
        print("__init__(self, ...):构造方法,在创建对象时自动调用。用于初始化对象的属性。")
        self.name = name
person = Person("Alice")  # 自动调用 __init__

call(self [, ...])

__call__ 方法允许一个类的实例像函数一样被调用。当你定义了这个方法后,你可以使用实例对象进行"调用",并触发该方法。以下是一个简单的示例:

python 复制代码
class Adder:
    def __init__(self, increment):
        self.increment = increment
    def __call__(self, value):
        return value + self.increment

# 创建一个 Adder 实例
add_five = Adder(5)
# 像函数一样调用实例
result = add_five(10)  # 自动调用 __call__
print(result)  # 输出: 15

getitem(self, key)

__getitem__ 方法允许你使用索引访问对象的元素。当你尝试访问对象的某个键或索引时,这个方法会被自动调用。以下是一个示例:

python 复制代码
class CustomList:
    def __init__(self, initial_data):
        self.data = initial_data
    def __getitem__(self, index):
        return self.data[index]
# 创建一个 CustomList 实例
my_list = CustomList([10, 20, 30, 40, 50])
# 使用索引访问元素
print(my_list[2])  # 输出: 30

输出结果为30

在这个例子中,CustomList 类实现了 __getitem__ 方法,使得我们可以像访问普通列表一样访问其内部的数据。这样,my_list[2] 实际上会调用 my_list.__getitem__(2),返回对应的元素。你对这个方法还有其他问题吗?

len(self)

__len__(self) 是一个特殊方法,用于定义一个对象的长度。通常在自定义类中使用,以便能够使用 len() 函数获取实例的长度。下面是一个简单的例子:

python 复制代码
class MyList:
    def __init__(self, items):
        self.items = items

    def __len__(self):
        return len(self.items)

# 示例
my_list = MyList([1, 2, 3, 4])
print(len(my_list))  # 输出: 4

在这个例子中,MyList 类实现了 __len__ 方法,使得我们可以通过 len(my_list) 获取其内部列表的长度。

repr(self) / str(self)

__repr__(self)__str__(self) 是两个特殊方法,用于定义对象的字符串表示。

  • __repr__ 旨在返回一个可以用来重新创建该对象的字符串,通常用于开发调试。
  • __str__ 则返回一个更易读的字符串,通常用于向用户显示。
python 复制代码
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        return f"Person(name='{self.name}', age={self.age})"

    def __str__(self):
        return f"{self.name}, {self.age} years old"

# 示例
person = Person("Alice", 30)
print(repr(person))  # 输出: Person(name='Alice', age=30)
print(str(person))   # 输出: Alice, 30 years old

在这个例子中,__repr__ 返回了一个包含对象信息的字符串,适合用于调试;而 __str__ 返回了一个更简洁、易读的字符串,用于用户友好的输出。

add(self, other)

__add__(self, other) 是一个特殊方法,用于定义对象之间的加法操作。通过实现这个方法,你可以自定义如何将两个对象相加。

下面是一个简单的例子,展示了如何使用 __add__ 方法:

python 复制代码
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        if isinstance(other, Vector):
            return Vector(self.x + other.x, self.y + other.y)
        return NotImplemented

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

# 示例
v1 = Vector(2, 3)
v2 = Vector(4, 5)
result = v1 + v2
print(result)  # 输出: Vector(6, 8)

在这个例子中,Vector 类实现了 __add__ 方法,使得可以直接使用 + 运算符将两个 Vector 对象相加。当 v1 + v2 被调用时,__add__ 方法被执行,返回一个新的 Vector 对象,其坐标是两个向量的坐标相加的结果。

radd(self, other)

__radd__(self, other) 是一个特殊方法,用于定义右加法操作,通常在左侧操作数不支持加法时被调用。它允许你在自定义对象的情况下实现与其他类型的加法。

下面是一个例子,展示了如何使用 __radd__ 方法:

python 复制代码
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        if isinstance(other, Vector):
            return Vector(self.x + other.x, self.y + other.y)
        return NotImplemented

    def __radd__(self, other):
        # 当其他对象调用 + 时,如果左边的对象不是 Vector
        return self.__add__(other)

    def __repr__(self):
        return f"Vector({self.x}, {self.y})"

# 示例
v1 = Vector(2, 3)
v2 = Vector(4, 5)

# 正常加法
result1 = v1 + v2
print(result1)  # 输出: Vector(6, 8)

# 使用数字与 Vector 相加
result2 = 1 + v1
print(result2)  # 输出: Vector(2, 3)

参考文献

相关推荐
数据智能老司机5 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机6 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机6 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机6 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i6 小时前
drf初步梳理
python·django
每日AI新事件6 小时前
python的异步函数
python
这里有鱼汤8 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook17 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室17 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三19 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试