如何使用pythonic方法获取实例f
的所有属性,并对刚刚获得的属性进行相同的操作。
无论顺序如何,我都想得到['__init__', 'own', 'Meta', 'field']
。
class Foo:
def __init__(self) -> None:
self.own = 'own'
class Meta:
field = None
f = Foo()
print(dir(f))
I want to get all attributes of f and the attributes of its subobjects.
if specific_attr in attrs:
do_something
当我使用dir(f)
时,我获得了太多价值,从中进行筛选似乎有些麻烦,并且没有attribute 'field'
,我需要再次使用dir(f.Meta)
。
我想要一种简单的方法来获取实例f
的所有属性及其子对象的属性,我的目的是:
if specific_attr in attrs: do_something
答案 0 :(得分:0)
尝试
print(dir(Foo))
这应该为您提供所需的属性列表。