相关疑难解决方法(0)

如何在python中动态创建类变量

我需要制作一堆类变量,我想通过循环遍历这样的列表来实现:

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

class Foo():
    for v in vars:
        setattr(no_idea_what_should_go_here,v,0)
Run Code Online (Sandbox Code Playgroud)

可能吗?我不想让它们成为一个实例(在__init__中使用self),而是作为类变量.

python class class-variables

19
推荐指数
3
解决办法
2万
查看次数

标签 统计

class ×1

class-variables ×1

python ×1