如果这个问题很明显,请原谅我,但根据我在 Python 的 OOP 教程中阅读的内容,它们都没有提到如何让静态变量存储静态方法。在我的代码中,我尝试了:
class Features:
a_static_variable = 1
a_static_variable_that_references_a_static_function = Features.func1
@staticmethod
def func1(blah):
print(blah)
尝试运行时我收到:
NameError: name 'Features' is not defined
类方法可以引用自己类中的静态方法吗?如果是这样,我该怎么做。我尝试用 nothing 和 self 替换 Features,但正如我所料,这些也没有意义。
请您参考如下方法:
这只是 func1 尚未定义的情况。
如果您重新排序,它应该可以工作:
class Features:
a_static_variable = 1
@staticmethod
def func1(blah):
print(blah)
a_static_variable_that_references_a_static_function = func1
