【发布时间】:2014-11-06 23:33:19
【问题描述】:
我有一堂课:
class CustomDictionary(dict):
def __init__(self, wrong_keys, *args, **kwargs):
super(CustomDictionary, self).__init__(*args, **kwargs)
self.__dict__ = self
self.wk = wrong_keys
print(self.wk)
########### other methods
def __setattr__(self, key, value):
print(self.wk) # error
key = key.replace(" ", "_")
self.__dict__[key] = value
我有这个类的客户:
def main():
wrong_keys = ["r23", "fwfew", "s43t"]
dictionary = CustomDictionary(wrong_keys)
dictionary.aws = 5
我在print(self.wk):KeyError: 'wk' 的行上有错误。另一方面,print(self.wk) 行成功打印了我的tuple。
我犯了什么错误?
【问题讨论】:
标签: inheritance python-3.x constructor