这是我复制的代码,我把它命名为mydict.py文件。代码中,我把__getattr__给注释掉了。
class Dict(dict): ''' Simple dict but also support access as x.y style. >>> d1 = Dict() >>> d1['x'] = 100 >>> d1.x 100 >>> d1.y = 200 >>> d1['y'] 200 >>> d2 = Dict(a=1, b=2, c='3') >>> d2.c '3' >>> d2['empty'] Traceback (most recent call last): ... KeyError: 'empty' >>> d2.empty Traceback (most recent call last): ... AttributeError: 'Dict' object has no attribute 'empty' ''' def __init__(self, **kw): super().__init__(**kw) #def __getattr__(self, key): # try: # return self[key] # except KeyError: # raise AttributeError(r"'Dict' object has no attribute '%s'" % key) def __setattr__(self, key, value): self[key] = value if __name__ == 'main': import doctest doctest.testmod()
在命令行里我输入了
python mydict.py
但是没有出现任何错误,请问这是为什么?
倒数第三行main函数错了,应该是__main__
Sign in to make a reply
第二十二军规
这是我复制的代码,我把它命名为mydict.py文件。代码中,我把__getattr__给注释掉了。
在命令行里我输入了
但是没有出现任何错误,请问这是为什么?