鱼C论坛

 找回密码
 立即注册
查看: 2174|回复: 2

[见证历程] python代码

[复制链接]
发表于 2020-4-11 21:08:00 | 显示全部楼层 |阅读模式

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

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

x
老版python 魔法算法:算法运算2代码
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] 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)

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

使用道具 举报

 楼主| 发表于 2020-4-11 21:08:45 | 显示全部楼层
含错误例子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 16:37:30 | 显示全部楼层
  1. >>> class int(int):
  2.         def __add__(self,other):
  3.                 return int.__sub__(self,other)

  4.         
  5. >>> a = int('5')
  6. >>> a
  7. 5
  8. >>> b = int(3)
  9. >>> a + b
  10. 2
  11. >>>
  12. =================================================== RESTART: Shell ==================================================
  13. >>> class Nint(int):
  14.         def __radd__(self,other):
  15.                 return int.__sub__(self,other)

  16.         
  17. >>> a = Nint(5)
  18. >>> b = Nint(3)
  19. >>> a + b
  20. 8
  21. >>> 1 + b
  22. 2
  23. >>>
  24. =================================================== RESTART: Shell ==================================================
  25. >>> class Nint(int):
  26.         def __rsub__(self,other):
  27.                 return int.__sub__(self,other)

  28.         
  29. >>> a = Nint(5)
  30. >>> 3 - a
  31. 2
  32. >>> class Nint(int):
  33.         def __rsub__(self,other):
  34.                 return int.__sub__(other,self)

  35.         
  36. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 12:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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