【发布时间】:2012-01-08 14:33:49
【问题描述】:
我需要创建一堆类变量,我想通过这样的列表循环来完成:
vars=('tx','ty','tz') #plus plenty more
class Foo():
for v in vars:
setattr(no_idea_what_should_go_here,v,0)
有可能吗?我不想将它们作为实例(在 __init__ 中使用 self),而是作为类变量。
【问题讨论】:
-
为什么不将它们保存在字典中?
-
您想在不同实例之间共享这些变量吗?
-
不确定。最有可能的。需求来自 API,我不知道它们是如何在其中使用的。
标签: python class class-variables