魔法方法是python内置方法,不需要主动调用,存在的目的是为了给python的解释器进行调用,几乎每个魔法方法都有一个对应的内置函数,或者运算符,当我们对这个对象使用这些函数或者运算符时就会调用类中的对应魔法方法
| 魔法方法 | 含义 |
|---|---|
| - | 基本的魔法方法 |
| __ new__(cls[, …]) | 1. __ new__ 是在一个对象实例化的时候所调用的第一个方法 2. 它的第一个参数是这个类,其他的参数是用来直接传递给 __ init__ 方法 3. __ new__ 决定是否要使用该 __ init__ 方法,因为 __ new__ 可以调用其他类的构造方法或者直接返回别的实例对象来作为本类的实例,如果 __ new__ 没有返回实例对象,则 __ init__ 不会被调用 4. __ new__ 主要是用于继承一个不可变的类型比如一个 tuple 或者 string |
| __ init__(self[, …]) | 构造器,当一个实例被创建的时候调用的初始化方法 |
| __ del__(self) | 析构器,当一个实例被销毁的时候调用的方法 |
| __ call__(self[, args…]) | 允许一个类的实例像函数一样被调用:x(a, b) 调用 x.__ call__(a, b) |
| __ len__(self) | 定义当被 len() 调用时的行为 |
| __ repr__(self) | 定义当被 repr() 调用时的行为 |
| __ str__(self) | 定义当被 str() 调用时的行为 |
| __ bytes__(self) | 定义当被 bytes() 调用时的行为 |
| __ hash__(self) | 定义当被 hash() 调用时的行为 |
| __ bool__(self) | 定义当被 bool() 调用时的行为,应该返回 True 或 False |
| __ format__(self, format_spec) | 定义当被 format() 调用时的行为 |
| - | 有关属性 |
| __ getattr__(self, name) | 定义当用户试图获取一个不存在的属性时的行为 |
| __ getattribute__(self, name) | 定义当该类的属性被访问时的行为 |
| __ setattr__(self, name, value) | 定义当一个属性被设置时的行为 |
| __ delattr__(self, name) | 定义当一个属性被删除时的行为 |
| __ dir__(self) | 定义当 dir() 被调用时的行为 |
| __ get__(self, instance, owner) | 定义当描述符的值被取得时的行为 |
| __ set__(self, instance, value) | 定义当描述符的值被改变时的行为 |
| __ delete__(self, instance) | 定义当描述符的值被删除时的行为 |
| - | 比较操作符 |
| __ lt__(self, other) | 定义小于号的行为:x < y 调用 x.__ lt__(y) |
| __ le__(self, other) | 定义小于等于号的行为:x <= y 调用 x.__ le__(y) |
| __ eq__(self, other) | 定义等于号的行为:x == y 调用 x.__ eq__(y) |
| __ ne__(self, other) | 定义不等号的行为:x != y 调用 x.__ ne__(y) |
| __ gt__(self, other) | 定义大于号的行为:x > y 调用 x.__ gt__(y) |
| __ ge__(self, other) | 定义大于等于号的行为:x >= y 调用 x.__ ge__(y) |
| - | 算数运算符 |
| __ add__(self, other) | 定义加法的行为:+ |
| __ sub__(self, other) | 定义减法的行为:- |
| __ mul__(self, other) | 定义乘法的行为:* |
| __ truediv__(self, other) | 定义真除法的行为:/ |
| __ floordiv__(self, other) | 定义整数除法的行为:// |
| __ mod__(self, other) | 定义取模算法的行为:% |
| __ divmod__(self, other) | 定义当被 divmod() 调用时的行为 |
| __ pow__(self, other[, modulo]) | 定义当被 power() 调用或 ** 运算时的行为 |
| __ lshift__(self, other) | 定义按位左移位的行为:<< |
| __ rshift__(self, other) | 定义按位右移位的行为:>> |
| __ and__(self, other) | 定义按位与操作的行为:& |
| __ xor__(self, other) | 定义按位异或操作的行为:^ |
| __ or__(self, other) | 定义按位或操作的行为: |
| - | 反运算 |
| __ radd__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rsub__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rmul__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rtruediv__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rfloordiv__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rmod__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rdivmod__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rpow__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rlshift__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rrshift__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rand__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ rxor__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| __ ror__(self, other) | (与上方相同,当左操作数不支持相应的操作时被调用) |
| - | 增量赋值运算 |
| __ iadd__(self, other) | 定义赋值加法的行为:+= |
| __ isub__(self, other) | 定义赋值减法的行为:-= |
| __ imul__(self, other) | 定义赋值乘法的行为:*= |
| __ itruediv__(self, other) | 定义赋值真除法的行为:/= |
| __ ifloordiv__(self, other) | 定义赋值整数除法的行为://= |
| __ imod__(self, other) | 定义赋值取模算法的行为:%= |
| __ ipow__(self, other[, modulo]) | 定义赋值幂运算的行为:**= |
| __ ilshift__(self, other) | 定义赋值按位左移位的行为:<<= |
| __ irshift__(self, other) | 定义赋值按位右移位的行为:>>= |
| __ iand__(self, other) | 定义赋值按位与操作的行为:&= |
| __ ixor__(self, other) | 定义赋值按位异或操作的行为:^= |
| __ ior__(self, other) | 定义赋值按位或操作的行为: |
| - | 一元操作符 |
| __ pos__(self) | 定义正号的行为:+x |
| __ neg__(self) | 定义负号的行为:-x |
| __ abs__(self) | 定义当被 abs() 调用时的行为 |
| __ invert__(self) | 定义按位求反的行为:~x |
| - | 类型转换 |
| __ complex__(self) | 定义当被 complex() 调用时的行为(需要返回恰当的值) |
| __ int__(self) | 定义当被 int() 调用时的行为(需要返回恰当的值) |
| __ float__(self) | 定义当被 float() 调用时的行为(需要返回恰当的值) |
| __ round__(self[, n]) | 定义当被 round() 调用时的行为(需要返回恰当的值) |
| __ index__(self) | 1. 当对象是被应用在切片表达式中时,实现整形强制转换2.如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __ index__3. 如果 __ index__ 被定义,则 __ int__ 也需要被定义,且返回相同的值 |
| - | 上下文管理(with 语句) |
| __ enter__(self) | 1.定义当使用 with 语句时的初始化行为2. __ enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定 |
| __ exit__(self, exc_type, exc_value, traceback) | 1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么2.一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作 |
| - | 容器类型 |
| __ len__(self) | 定义当被 len() 调用时的行为(返回容器中元素的个数) |
| __ getitem__(self, key) | 定义获取容器中指定元素的行为,相当于 self[key] |
| __ setitem__(self, key, value) | 定义设置容器中指定元素的行为,相当于 self[key] = value |
| __ delitem__(self, key) | 定义删除容器中指定元素的行为,相当于 del self[key] |
| __ iter__(self) | 定义当迭代容器中的元素的行为 |
| __ reversed__(self) | 定义当被 reversed() 调用时的行为 |
| __ contains__(self, item) | 定义当使用成员测试运算符(in 或 not in)时的行为 |
本文详细介绍了Python中的魔法方法,包括它们的基本用途、属性相关的方法、比较及算术运算符的重载方式,以及如何通过魔法方法实现上下文管理和容器类型的操作。
367

被折叠的 条评论
为什么被折叠?



