how to create class variable dynamically in python

前端 未结 4 1868
再見小時候
再見小時候 2021-01-31 18:12

I need to make a bunch of class variables and I would like to do it by looping through a list like that:

vars=(\'tx\',\'ty\',\'tz\') #plus plenty more

class Foo         


        
4条回答
  •  深忆病人
    2021-01-31 19:08

    Late to the party but use the type class constructor!

    Foo = type("Foo", (), {k: 0 for k in ("tx", "ty", "tz")})
    

提交回复
热议问题