0
我有一個類:如何在__setattr__函數中獲取構造函數參數?
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
。
我做了什麼錯誤?
感謝您的幫助! – Denis 2014-11-08 01:24:00