检查属性是否为(继承)属性

时间:2017-01-12 15:17:14

标签: python inheritance properties

我正在使用具有大量自定义__getattrs____setattrs__的自定义框架。基本上,如果属性名称未指定,则会引发错误。我仍然希望能够添加属性。

到目前为止,我已将 setattr 编辑为

elif isinstance(self.__class__.__dict__[name], property):
    object.__setattr__(self, name, value)

但这失败了,涉及继承。是否有一些很好的python解决方案,或者我只需要通过所有父母来解决,直到我找到或找不到该属性?

编辑:澄清上面的行Basically, if the attribute name isn't something specified, it throws and error

list_a = ['attr1', 'attr2']
list_b = ['attr3', 'attr4']

def __getattr__(self, name):
    if name in list_a:
        result = list_a_action(name)
    elif name in list_b:
        result = list_b_action(name)
    else:
        raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))

0 个答案:

没有答案