How to create a new unknown or dynamic/expando object in Python

后端 未结 5 402
我在风中等你
我在风中等你 2021-02-02 07:07

In python how can we create a new object without having a predefined Class and later dynamically add properties to it ?

example:

dynamic_object = Dynami         


        
5条回答
  •  野性不改
    2021-02-02 07:39

    Just define your own class to do it:

    class Expando(object):
        pass
    
    ex = Expando()
    ex.foo = 17
    ex.bar = "Hello"
    

提交回复
热议问题