|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 风不会停息 于 2018-7-5 14:37 编辑
1. python3以后, int(), float(), list(), str()等工厂函数的调用实际上就是创建类的实例化对象的过程, 工厂函数就是类对象
2. 当对实例对象进行算术运算时, 会自动调用算术运算的魔法方法
3.
- 运算符 对应的魔法方法
- + __add__(self, other)
- - __sub__(self, other)
- * __mul__(self. other)
- / __truediv__(self, other)
- // __floordiv__(slef, other)
- % __mod__(self, other)
- ** __pow__(self, other[, modulo])
- << __lshift__(self, other)
- >> __rshift__(slef, other)
- & __and__(self, other)
- ^ __xor__(self, other)
- | __or__(self, other)
复制代码
4. 例如, 当 a + b 时, 会自动调用 __add__() 方法, 可以通过修改魔法方法来自定义算术运算 |
|