【发布时间】:2019-09-20 03:41:30
【问题描述】:
我的类中有类属性,我想动态设置,这是一个代码示例
class Something:
attribute1 = 42 # this is shared between all class instances
def _init_(self, value):
self.value = value
我的问题是:有没有办法将该类属性 (attribute1) 设置为某个值,就像我可以像这样设置对象实例属性一样:
something = Something(value)
【问题讨论】:
-
Something.attribute1 = somevalue? -
尝试使用@classmethod
-
注意
_init_应该是__init__。
标签: python python-2.7 class-attributes