您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在python中遍历一个类的所有成员变量

在python中遍历一个类的所有成员变量

dir(obj)

为您提供对象的所有属性。您需要自己从方法等中过滤出成员:

class Example(object):
    bool143 = True
    bool2 = True
    blah = False
    foo = True
    foobar2000 = False

example = Example()
members = [attr for attr in dir(example) if not callable(getattr(example, attr)) and not attr.startswith("__")]
print members

会给你:

['blah', 'bool143', 'bool2', 'foo', 'foobar2000']
python 2022/1/1 18:48:41 有386人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶