python代码
老版python 魔法算法:算法运算2代码Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> class int(int):
def __add__(self,other):
return int.__sub__(self,other)
>>> a = int('5')
>>> a
5
>>> b = int(3)
>>> a + b
2
>>>
=================================================== RESTART: Shell ==================================================
>>> class Nint(int):
def __radd__(self,other):
return int.__sub__(self,other)
>>> a = Nint(5)
>>> b = Nint(3)
>>> a + b
8
>>> 1 + b
2
>>>
=================================================== RESTART: Shell ==================================================
>>> class Nint(int):
def __rsub__(self,other):
return int.__sub__(self,other)
>>> a = Nint(5)
>>> 3 - a
2
>>> class Nint(int):
def __rsub__(self,other):
return int.__sub__(other,self)
>>> 含错误例子 >>> class int(int):
def __add__(self,other):
return int.__sub__(self,other)
>>> a = int('5')
>>> a
5
>>> b = int(3)
>>> a + b
2
>>>
=================================================== RESTART: Shell ==================================================
>>> class Nint(int):
def __radd__(self,other):
return int.__sub__(self,other)
>>> a = Nint(5)
>>> b = Nint(3)
>>> a + b
8
>>> 1 + b
2
>>>
=================================================== RESTART: Shell ==================================================
>>> class Nint(int):
def __rsub__(self,other):
return int.__sub__(self,other)
>>> a = Nint(5)
>>> 3 - a
2
>>> class Nint(int):
def __rsub__(self,other):
return int.__sub__(other,self)
>>>
页:
[1]