第二章 魔法方法
魔法方法大全,参考于:Python 魔法方法详解
https://fishc.com.cn/thread-48793-1-1.html,做了补充和重翻译,删除部分不常用内容。
魔法方法在类中使用。
| 魔法方法 | 如何使用 | |
|---|---|---|
| len(self) | 定义当被 len() 调用时的行为 | |
| repr(self) | 定义当被 repr() 调用时的行为 | |
| str(self) | 定义当被 str() 调用时的行为 | |
| bytes(self) | 定义当被 bytes() 调用时的行为 | |
| hash(self) | 定义当被 hash() 调用时的行为 | |
| bool(self) | 定义当被 bool() 调用时的行为,应该返回 True 或 False | |
| format(self, format_spece) | 定义当被 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[, model]) | 定义当被 power() 调用或 ** 运算时的行为 | |
| lshift(self, other) | 定义按位左移位的行为:<< | |
| rshift(self, other) | 定义按位右移位的行为:>> | |
| and(self, other) | 定义按位与操作的行为:& | |
| xor(self, other) | 定义按位异或操作的行为:^ | |
| or(self, other) | 定义按位或操作的行为: | |
| 类型 | 反运算 | |
| radd(self, other) | (与add相同,当左操作数不支持相应的操作时被调用) | |
| rsub(self, other) | (与sub相同,当左操作数不支持相应的操作时被调用) | |
| rmul(self, other) | (与mul相同,当左操作数不支持相应的操作时被调用) | |
| rtruediv(self, other) | (与truediv相同,当左操作数不支持相应的操作时被调用) | |
| rfloordiv(self, other) | (与floordiv相同,当左操作数不支持相应的操作时被调用) | |
| rmod(self, other) | (与mod相同,当左操作数不支持相应的操作时被调用) | |
| rdivmod(self, other) | (与divmod相同,当左操作数不支持相应的操作时被调用) | |
| rpow(self, other) | (与pow相同,当左操作数不支持相应的操作时被调用) | |
| rlshift(self, other) | (与lshift相同,当左操作数不支持相应的操作时被调用) | |
| rrshift(self, other) | (与rshift相同,当左操作数不支持相应的操作时被调用) | |
| rand(self, other) | (与and相同,当左操作数不支持相应的操作时被调用) | |
| rxor(self, other) | (与xor相同,当左操作数不支持相应的操作时被调用) | |
| ror(self, other) | (与or相同,当左操作数不支持相应的操作时被调用) | |
| 其它 | 反操作就是左边数不能操作时用的,例如l = 1313 不能说1313 = l | |
| 类型 | 增量赋值运算 | |
| 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() 调用时的行为(需要返回恰当的值) | |
| 类型 | 容器 | |
| 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)时的行为 |










网友评论