python魔法方法大全
在Python中间,所有以__双下划线包裹的方法统称为Magic Method(魔法方法),如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,这边给大家把所有的魔术方法汇总了一下,希望对大家的学习有所帮助。
一、基本魔法方法
方法 |
说明 |
__new__(cls[, ...]) |
创建对象的方法是通过类创建对象时调用__new__ 创建方法 |
__init__(self[, ...]) |
创建实例时调用的初始化方法 |
__del__(self) |
当实例被销毁时,分析方法被调用 |
__call__(self[, args...]) |
允许像函数一样调用一类实例:x(a, b) 调用 x.__call__ (a, b) |
__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) |
当描述符的值被删除时,定义行为 |
三、上下文管理器
方法 |
说明 |
__enter__ (self) |
定义当使用 with 上下文管理器执行语句的方法 ,__enter__ 的返回值被 with 语句的目标或 as 绑定后面的名字 |
__exit__ (self, exc_type, exc_value, traceback) |
当with中间代码块执行后调用的方法。一般用于处理异常、清除工作或做一些代码块执行后的日常工作 |
四、与容器类型数据相关
方法 |
说明 |
__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)时的行为 |
五、比较运算符
方法 |
说明 |
__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) |
定义当被 round() 调用时的行为(需要返回恰当的值) |
__index__(self) |
1. 当对象是被应用在切片表达式中时,实现整形强制转换 2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__ 3. 如果 __index__ 被定义,则 int 也需要被定义,且返回相同的值 |