source:
- Data Model
- Book: Fluent Python
表1-1:
** 跟运算符无关的特殊方法 **
** Special method names (operators excluded)**
类别 | Category | 方法名(Method names) |
---|---|---|
字符串 / 字节序列表示形式 | String/bytes representation | __repr__ __str__ __format__ __bytes__ |
数值转换 | Conversion to number | __abs__ __bool__ __complex__ __int__ __float__ __hash__ __index__ |
集合模拟 | Emulating collections | __len__ __getitem__ __setitem__ __delitem__ __contains__ |
迭代枚举 | Iteration | __iter__ __reversed__ __next__ |
可调用模拟 | Emulating callables | __call__ |
上下文管理 | Context management | __enter__ __exit__ |
实例创建和销毁 | Instance creation and destruction | __new__ __init__ __del__ |
属性管理 | Attribute management | __getattr__ __getattribute__ __setattr__ __delattr__ __dir__ |
属性描述符 | Attribute descriptors | __get__ __set__ __delete__ |
跟类相关的服务 | Class services | __prepare__ __instancecheck__ __subclasscheck__ |
表1-2:
** 跟运算符相关的特殊方法 **
** Special method names for operators **
类别 | Category | 方法名和对应的运算符(Method names and related operators) |
---|---|---|
一元运算符 | Unary numeric operators | __neg__ ← - __pos__ ← + __abs__ ← abs() |
众多比较运算符 | Rich comparison operators | __lt__ ← < __le__ ← <= __eq__ ← == __ne__ ← != __gt__ ← > __ge__ ← >= |
算术运算符 | Arithmetic operators | __add__ ← + __sub__ ← - __mul__ ← * __truediv__ ← / __floordiv__ ← // __mod__ ← % __divmod__ ← divmod() __pow__ ← ** __round__ ← round() |
反向算术运算符 | Reversed arithmetic operators | __radd__ __rsub__ __rmul__ __rtruediv__ __rfloordiv__ __rmod__ __rdivmod__ __rpow__ |
增量赋值算术运算符 | Augmented assignment arithmetic operators | __iadd__ __isub__ __imul__ __itruediv__ __ifloordiv__ __imod__ __ipow__ |
位运算符 | Bitwise operators | __invert__ ← ~ __lshift__ ← << __rshift__ ← >> __and__ ← & __or__ ← | __xor__ ← ^ |
反向位运算符 | Reversed bitwise operators | __rlshift__ __rrshift__ __rand__ __rxor__ __ror__ |
增量赋值位运算符 | ugmented assignment bitwise operators | __ilshift__ __irshift__ __iand__ __ixor__ __ior__ |