深切进修Python之魔法要领【Python教程】,Python,魔法方法

什么是Python魔法要领
魔法要领就犹如它的名字一样奇异,总能在你须要的时刻为你供应某种要领来让你的主意完成。魔法要领是指Python内部已包括的,被双下划线所围困的要领,这些要领在举行特定的操纵时会自动被挪用,它们是Python面向对象下伶俐的结晶。初学者控制Python的魔法要领也就变得尤为重要了。
为何要运用Python魔法要领
运用Python的魔法要领能够使Python的自由度变得更高,当不须要重写时魔法要领也能够在划定的默许情况下见效,在须要重写时也能够让运用者依据本身的需求来重写部份要领来到达本身的期待。而且尽人皆知Python是支撑面向对象的言语Python的基本魔法要领就使得Python在面临对象方面做得更好。
魔法要领名 |
申明 |
基本魔法要领(较为经常使用) |
|
__new__(cls[, ...]) | 1.实例化对象时第一个被挪用的要领 2.其参数直接传递给__init__要领处置惩罚 3.我们平常不会重写该要领 |
__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) | 当被运算对象(左侧的操纵对象)不支撑该运算时被挪用 |
__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) | 定义赋值按位或操纵的行动:|= |
一元操纵符 | |
__neg__(self) | 定义正号的行动:+x |
__pos__(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视频教程
以上就是深切进修Python之魔法要领的细致内容,更多请关注ki4网别的相干文章!