本文正在参加「Python主题月」,详情查看 活动链接
基本的魔法方法
| 名称 | 解释 |
|---|
| _new_(cls[, ...]) | 在一个对象实例化的时候所调用的第一个方法 |
| _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)时的行为 |