【发布时间】:2019-12-27 10:29:09
【问题描述】:
我想定义一个类,它可以为从它请求的任何属性返回一些东西,这可能吗? 例如
class AnyAttr(object):
def __init__(self):
<somehow return the attr str for any str>
aa = AnyAttr()
print aa.cat
print aa.dog
Out [1]: 'cat', 'dog'
当然,它应该能够返回其他内容,例如在不同的字典中查找它们并返回它找到的内容或默认值。 有什么办法可以做到吗?
【问题讨论】:
-
定义
__getattr__?
标签: python object attributes attributeerror