Set attributes from dictionary in python

后端 未结 7 1685
南旧
南旧 2020-11-27 10:50

Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object?

Something like this:

 d =          


        
7条回答
  •  悲&欢浪女
    2020-11-27 11:18

    say for example

    class A():
        def __init__(self):
            self.x=7
            self.y=8
            self.z="name"
    

    if you want to set the attributes at once

    d = {'x':100,'y':300,'z':"blah"}
    a = A()
    a.__dict__.update(d)
    

提交回复
热议问题