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

后端 未结 5 380
我在风中等你
我在风中等你 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:42

    One way that I found is also by creating a lambda. It can have sideeffects and comes with some properties that are not wanted. Just posting for the interest.

    dynamic_object = lambda:expando
    dynamic_object.dynamic_property_a = "abc"
    dynamic_object.dynamic_property_b = "abcdefg"
    

提交回复
热议问题