Shortest way of creating an object with arbitrary attributes in Python?

前端 未结 11 990
谎友^
谎友^ 2021-02-01 14:48

Hey, I just started wondering about this as I came upon a code that expected an object with a certain set of attributes (but with no specification of what type this object shoul

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 15:25

    If you don't need to pass values in the constructor, you can do this:

    class data: pass
    
    data.foo = 1
    data.bar = 2
    

    You use the class static member variables to hold your data.

提交回复
热议问题