values已经定义,那是self.values
>>> class Countlist:
def __init__(self,*args): #*args 用来将参数打包成tuple给函数体调用
self.values = [x for x in args]
self.count ={}.fromkeys(range(len(self.values)),0)
#Python 字典 fromkeys() 函数用于创建一个新字典,
#以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。
def __len__(self):
return len(self,values)
def __getitem__(self,key):
self.count[key] +=1
return self.values[key]
>>> len(Countlist())
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
len(Countlist())
File "<pyshell#12>", line 10, in __len__
return len(self,values)
NameError: name 'values' is not defined
你告诉我不报错? |