python 动态定义类属性
“””
调用模块
“””
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])
结果: {‘att4’: 2, ‘att1’: 1, ‘att3’: 10, ‘att2’: 100} 1