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)

参考文献

相关推荐
EntyIU1 小时前
mineru从安装部署到测试使用完整指南
python·ocr
安替-AnTi2 小时前
厚朴 APK 搜索接口分析
python·apk·解析·taobao
山川湖海2 小时前
AI时代快速学编程语言的陷阱(以Python为例)
大数据·人工智能·python
H Journey2 小时前
Supervisor 进程管理工具介绍
python·supervisor·linux 运维
春日见3 小时前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
DeniuHe3 小时前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
DeniuHe3 小时前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
隐于花海,等待花开4 小时前
16.Python 常用第三方库概览 深度解析
python
我材不敲代码4 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python
风落无尘4 小时前
第十一章《对齐与安全》 完整学习资料
python·安全·机器学习