Create an anonymous class instance in python

前端 未结 5 1551
礼貌的吻别
礼貌的吻别 2020-12-29 04:44

Sometimes i need to create an anonymous class instance in python, just like c#:

var o= new {attr1=\"somehing\", attr2=344};

but in python i

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 05:15

    If you are using Python 3.3 or later, you can use types.SimpleNamespace:

    from types import SimpleNamespace
    
    o = SimpleNamespace(attr1="something", attr2=344)
    print(o)
    
    # namespace(attr1='something', attr2=344)
    

提交回复
热议问题