不使用属性,而是实现以下方法:
__getattr__(self, name)
__setattr__(self, name, value)
__delattr__(self, name)
请参阅http://docs.python.org/reference/datamodel.html#customizing-attribute-
access
您的
__getattr__方法可能如下所示:
def __getattr__(self, name): try: return self.__dict[name] except KeyError: msg = "'{0}' object has no attribute '{1}'" raise AttributeError(msg.format(type(self).__name__, name))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)