Home  >  Article  >  Backend Development  >  Python dynamically defines class attributes

Python dynamically defines class attributes

巴扎黑
巴扎黑Original
2016-12-09 09:50:531156browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python backup fileNext article:python backup file