|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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) 定义按位或操作的行为: | |
评分
-
查看全部评分
|