有人知道如何在类属性中引用类静态方法吗?例如:
class A(object):
var = A.staticMethod1 # this line doesn't work, as A has not be defined yet.
@staticmethod
def staticMethod1(*param):
pass
答案 0 :(得分:2)
类块只是临时作用域,因此,它实际上非常简单。只需将别名移到方法定义下方,然后删除类名(因为您直接在范围内)
class A(object):
@staticmethod
def staticMethod1(*param):
pass
var = staticMethod1