鱼C论坛

 找回密码
 立即注册
查看: 1219|回复: 5

[已解决]12.6定制序列里的__len__魔法方法

[复制链接]
发表于 2020-6-18 11:37:51 | 显示全部楼层 |阅读模式

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

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

x
代码如下:
class Count:
    def __init__(self,*num):
        self.list1=[x for x in  num]
        self.dict1={}.fromkeys(range(len(self.list1)),0)
    def __len__(self):
        super(). __len__(self.list1)
    def __getitem__(self,key):
        self.dict1[key]+=1
        return self.list1[key]
    def __setitem__(self,key,value):
        self.dict1[len(self.list1)+1]=0
        self.list1.append(value)
    def __delitem__(self,key):
        self.dict1.pop(key)
        self.list1.pop(key)
        self.dict1=dict(zip(list(range(len(self.list1))),list(self.dict1.values)))
    def reverse(self):
        self.list1.reverse()
        list2=list(self.dict1.values).reverse()
        self.dict1=dict(zip(list(range(len(self.list1))),list2))
    def counter(self,key):
        return self.dict1[key]
为什么def __len__(self):
        super(). __len__(self.list1)   会报错显示AttributeError: 'super' object has no attribute '__len__'?有没有大神知道的
        
最佳答案
2020-6-18 17:29:57
没有指定继承的类时会默认继承object类

所以Count继承于object,而object没有__len__方法
所以用super().__len__(self.list1)就会报错

如果是
return self.list1.__len__()
因为self.list1是list类的实例,而list有__len__方法
所以这个代码就是调用了list类的__len__方法
不会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-18 11:44:54 | 显示全部楼层


这样就不会报错了:
  1. class Count:
  2.     def __init__(self,*num):
  3.         self.list1=[x for x in  num]
  4.         self.dict1={}.fromkeys(range(len(self.list1)),0)
  5.     def __len__(self):
  6.         print('自动调用了 len 方法~')
  7.         return self.list1.__len__()
  8.     def __getitem__(self,key):
  9.         self.dict1[key]+=1
  10.         return self.list1[key]
  11.     def __setitem__(self,key,value):
  12.         self.dict1[len(self.list1)+1]=0
  13.         self.list1.append(value)
  14.     def __delitem__(self,key):
  15.         self.dict1.pop(key)
  16.         self.list1.pop(key)
  17.         self.dict1=dict(zip(list(range(len(self.list1))),list(self.dict1.values)))
  18.     def reverse(self):
  19.         self.list1.reverse()
  20.         list2=list(self.dict1.values).reverse()
  21.         self.dict1=dict(zip(list(range(len(self.list1))),list2))
  22.     def counter(self,key):
  23.         return self.dict1[key]
  24. c = Count(1,2,3,4,5)
  25. print(len(c))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-18 12:12:41 | 显示全部楼层
Twilight6 发表于 2020-6-18 11:44
这样就不会报错了:

这是为什么啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-18 12:35:05 | 显示全部楼层
本帖最后由 Twilight6 于 2020-6-18 13:29 编辑


具体我也不知道,刚刚去查了资料也没查到什么说明  

只是调用 len 函数时候会自动调用 __len__ 方法,你可以重写随意设置返回值多少
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-18 16:53:12 | 显示全部楼层
直接用len函数, return len(obj)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-18 17:29:57 | 显示全部楼层    本楼为最佳答案   
没有指定继承的类时会默认继承object类

所以Count继承于object,而object没有__len__方法
所以用super().__len__(self.list1)就会报错

如果是
return self.list1.__len__()
因为self.list1是list类的实例,而list有__len__方法
所以这个代码就是调用了list类的__len__方法
不会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 02:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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