class One:
i = One.get(9)
@staticmethod
def get(val):
pass
我尝试使用静态方法初始化静态变量,但上面的代码引发了此错误:
NameError: name 'One' is not defined
如何在 Python 中使用静态方法初始化静态变量?
最佳答案
class One:
@staticmethod
def get(val):
pass
i = get.__func__(9)
但这可能不是最Pythonic的方式。请注意 i
变量位于 get
声明之后。自 @staticmethod
不可直接调用(如果这样做,您将收到一条消息),您必须执行底层函数( __func__
)。
关于python - 如何使用静态方法初始化静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34020795/