鱼C论坛

 找回密码
 立即注册
查看: 1612|回复: 3

[已解决]关于定制序列的一个

[复制链接]
发表于 2017-9-6 18:00:00 | 显示全部楼层 |阅读模式

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

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

x
  1. class Mytuple:
  2.     def __init__(self,*args):
  3.         self.length=0
  4.         self.args=args
  5.         for i in args:
  6.             self.length+=1
  7.     def __len__(self):
  8.         print(self.length)#这里出错了
  9.     def __getitem__(self,key):
  10.         count=0
  11.         for i in self.args:
  12.             
  13.             if count==key:
  14.                 return(i)
  15.             count+=1
  16. m=Mytuple(1,2,3)
  17. len(m)
复制代码

RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python36-32/定制自己的不可变序列.py
3
Traceback (most recent call last):
  File "C:/Users/ASUS/AppData/Local/Programs/Python/Python36-32/定制自己的不可变序列.py", line 17, in <module>
    len(m)
TypeError: 'NoneType' object cannot be interpreted as an integer
自己不能理解这个错误,请大家帮助
最佳答案
2017-9-7 09:51:50
本帖最后由 ba21 于 2017-9-7 09:53 编辑
小六12 发表于 2017-9-7 00:45
就是想问为什么print会出错啊?



我也想知道,人家要返回值,你为什么偏偏就不给返回值;而且还莫名的问print不行

  1. class Mytuple:
  2.     def __init__(self,*args):
  3.         self.length=0
  4.         self.args=args
  5.         for i in args:
  6.             self.length+=1
  7.     def __len__(self):
  8.         print(self.length)#这里出错了
  9.         return self.length#这里出错了
  10.     def __getitem__(self,key):
  11.         count=0
  12.         for i in self.args:
  13.             
  14.             if count==key:
  15.                 return(i)
  16.             count+=1
  17. m=Mytuple(1,2,3)
  18. len(m)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-6 18:37:12 | 显示全部楼层
  return self.length#这里出错了

这不就可以了。还print
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-7 00:45:05 | 显示全部楼层
ba21 发表于 2017-9-6 18:37
return self.length#这里出错了

这不就可以了。还print

就是想问为什么print会出错啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-7 09:51:50 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ba21 于 2017-9-7 09:53 编辑
小六12 发表于 2017-9-7 00:45
就是想问为什么print会出错啊?



我也想知道,人家要返回值,你为什么偏偏就不给返回值;而且还莫名的问print不行

  1. class Mytuple:
  2.     def __init__(self,*args):
  3.         self.length=0
  4.         self.args=args
  5.         for i in args:
  6.             self.length+=1
  7.     def __len__(self):
  8.         print(self.length)#这里出错了
  9.         return self.length#这里出错了
  10.     def __getitem__(self,key):
  11.         count=0
  12.         for i in self.args:
  13.             
  14.             if count==key:
  15.                 return(i)
  16.             count+=1
  17. m=Mytuple(1,2,3)
  18. len(m)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-2 04:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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