如何在当前类变量中引用类静态方法?

时间:2018-12-21 03:18:59

标签: python

有人知道如何在类属性中引用类静态方法吗?例如:

class A(object):

    var = A.staticMethod1   # this line doesn't work, as A has not be defined yet.

    @staticmethod
    def staticMethod1(*param):
        pass

1 个答案:

答案 0 :(得分:2)

类块只是临时作用域,因此,它实际上非常简单。只需将别名移到方法定义下方,然后删除类名(因为您直接在范围内)

class A(object):

    @staticmethod
    def staticMethod1(*param):
        pass

    var = staticMethod1