鱼C论坛

 找回密码
 立即注册
查看: 659|回复: 1

请问课后作业41讲里这个代码为什么要继承于int类?

[复制链接]
发表于 2018-7-13 12:15:06 | 显示全部楼层 |阅读模式

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

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

x
  1. class Nint(int):
  2.         def __new__(cls, arg=0):
  3.                 if isinstance(arg, str):
  4.                         total = 0
  5.                         for each in arg:
  6.                                 total += ord(each)
  7.                         arg = total
  8.                 return int.__new__(cls, arg)
复制代码



错误类型是TypeError: int.__new__(Newint): Newint is not a subtype of int

代码最后一行里对int类调用new方法是什么意思啊,为什么这个Nint类一定要继承于int类才能调用它呢?



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-7-13 12:54:37 | 显示全部楼层
继承搜索树规则,希望你能了解一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-31 17:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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