Python学习之路(8)——魔法方法汇总
在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()调用时的行为,返回布尔值 |
|
__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[, modulo]) |
(与运算操作相同,当左操作数不支持相应的操作时被调用) |
|
__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) |
|
|
|
上下文管理(with语句) |
|
__enter__(self) |
|
|
__exit__(self, exc_type, exc_value, traceback) |
|
|
|
容器类型 |
|
__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)时的行为 |
可以新创建一个类,并对父类继承的算术魔法方法进行重写,如下:
>>> class NewInt(int): #对NewInt对象的加减法互换 def __add__(self, other): #实际上__add__是减法 return int.__sub__(self, other) def __sub__(self, other): #实际上__sub__是加法 return int.__add__(self, other) >>> a = NewInt(16) >>> a 16 >>> a + 8 8 >>> a - 8 24
浙公网安备 33010602011771号