遍历python中的对象属性

python开发中有时候需要遍历某对象的属性list:

class Person(object):
    """
    职员信息
    """
    name = 0
    age = 1
    id = 2
    group = 3


attr = [a for a in dir(Person) if not a.startswith('__')]
for a in attr:
    print(a, getattr(Person, a))

"""
age 1
group 3
id 2
name 0
"""

你可能感兴趣的:(python,1024程序员节)