heiyezhiyu 发表于 2020-4-11 21:08:00

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)

       
>>>

heiyezhiyu 发表于 2020-4-11 21:08:45

含错误例子

heiyezhiyu 发表于 2020-4-17 16:37:30

>>> 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]
查看完整版本: python代码