鱼C论坛

 找回密码
 立即注册
查看: 2118|回复: 0

[学习笔记] 042魔法方法:算术运算

[复制链接]
发表于 2017-7-18 22:03:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.工厂函数
>>> type(len)
<class 'builtin_function_or_method'>
>>> type(dir)
<class 'builtin_function_or_method'>
>>> type(int)
<class 'type'>
>>> type(list)
<class 'type'>
>>> class C:
        pass

>>> type(C)
<class 'type'>
>>> a = int('123')
>>> a
123
>>> b = int('456')
>>> a + b
579
>>> class New_int(int):
        def __add__(self, other):
                return int.__sub__(self, other)
        def __sub__(self, other):
                return int.__add__(self, other)

        
>>> a = New_int(3)
>>> b = New_int(5)
>>> a + b
-2
>>> a - b
8
>>> class Try_int(int):
        def __add__(self, other):
                return self + other
        def __sub__(self, other):
                return self - other

        
>>> a = Try_int(3)
>>> b = Try_int(5)
>>> a + b
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    a + b
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
  File "<pyshell#23>", line 3, in __add__
    return self + other
RuntimeError: maximum recursion depth exceeded
>>> class Try_int(int):
        def __add__(self, other):
                return int(self) + int(other)
        def __sub__(self, other):
                return int(self) - int(other)

        
>>> a = Try_int(3)
>>> b = Try_int(5)
>>> a + b
8
2.算术运算符
__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)                      定义按位或操作的行为: |

评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-24 13:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表