Home >Backend Development >Python Tutorial >Python dynamically defines class attributes
Python dynamically defines class attributes
""" 调用模块 """ if __name__=='__main__': import person att= {'att1':1,'att2':100,'att3':10,'att4':2} p = person.person(att) print(p.__dict__) print(p.att1)
""" 类 person.py """ class person(object): def __init__(self,att): for o in att: setattr(self,o,att[o])
Result:
{'att4': 2, 'att1': 1, 'att3': 10, 'att2': 100}
1