如何使用其他静态成员定义静态类成员?例如:
class Somefuncs:
@staticmethod
def foo():
print('foo was called')
functs_dict={'foo':Somefuncs.foo}
使解释器引发异常:未解析的引用'Somefuncs'即使我在Somefuncs类中定义funct_dict!
至此,该类尚未真正定义。
但是您可以写:
class Somefuncs:
@staticmethod
def foo():
print('foo was called')
functs_dict={'foo': foo}
# Test it
Somefuncs.functs_dict["foo"]()
# Output: foo was called
它总是指当前类别