Python dynamically defines class attributes

巴扎黑
Release: 2016-12-09 09:50:53
Original
1155 people have browsed it

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)
Copy after login
"""
类 person.py
"""
class person(object):
    def __init__(self,att):
        for o in att:
            setattr(self,o,att[o])
Copy after login

Result:
{'att4': 2, 'att1': 1, 'att3': 10, 'att2': 100}
1

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template