流水沉微

Python

@staticmethod vs. @classmethod

2019-10-02

主要来源:Stack Overflow

class A(object):
    def foo(self, x):
        print "executing foo(%s, %s)" % (self, x)

    @classmethod
    def class_foo(cls, x):
        print "executing class_foo(%s, %s)" % (cls, x)

    @staticmethod
    def static_foo(x):
        print "executing static_foo(%s)" % x    

a = A()

a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>,1)


a.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)


A.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)


a.static_foo(1)
# executing static_foo(1)


A.static_foo('hi')
# executing static_foo(hi)

可以认为foo是实例方法,class_foo是类方法,而static_foo是静态方法

  • foo 会隐式传递参数selfself是当前调用这个方法的实例: a

  • class_foo 会隐式传递参数clscls是当前调用这个方法的类: A

  • static_foo 什么都没有,显示传递给它什么参数它就有什么,可以看成普通的 module function

1 Jan 2016