【发布时间】:2014-05-27 11:41:57
【问题描述】:
来自 C++,我无法使用静态成员方法。
class Color():
def __init__(self):
print ("red")
@staticmethod
def cl(val):
return float(val / Color.colorConstant)
red = (Color.cl(1.0), 0.0, 0.0)
green = (0.0, 1.0, 0.0)
blue = (1.0, 0.0, 1.0)
purple = (1.0, 0.0, 0.0)
colorConstant = 255
我收到错误:name 'Color' is not defined # on line 9
red = (Color.cl(1.0, 0.0, 0.0))
这段代码应该正常工作吗?我想在定义成员属性的时候调用一个静态方法。
【问题讨论】:
-
您的代码在您的帖子中是否正确缩进?
-
错误是在
red = (Color.cl(1.0), 0.0, 0.0)这一行还是return float(val / Color.colorConstant)这一行? -
我现在编辑了缩进。 @MaximeLorant。它与 ''red = (Color.cl(1.0), 0.0, 0.0)'' 一致
-
您使用的是哪个版本的 Python?
-
当我运行你的代码时,我得到一个不同的错误:
AttributeError: class Color has no attribute 'colorConstant'
标签: python static static-methods