Python内置函数(11)-classmethod

官方文档:

    Transform a method into a class method.

    A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

    The @classmethod form is a function decorator – see the description of function definitions in Function definitions for details.

     It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

     Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.

      For more information on class methods, consult the documentation on the standard type hierarchy in The standard type hierarchy

说明:1.classmethod是一个装饰器函数,用来表示一个方法为类方法

         2.类方法的第一个参数是类对象参数,在方法被调用时自动传入类对象,类参数名约定为cls

         3.如果一个方法被表示为类方法,则该方法可被类对象调用(如C.f()),也可以被类的实例对象调用(如 C().f())

 

class C:
    @classmethod
    def f(cls,args):
        print(cls)
        print(args)
C.f('类对象调用类方法')
#输出
# <class '__main__.C'>
# 类对象调用类方法

c = C()
c.f('类实例对象调用类方法')
#输出
#<class '__main__.C'>
#类实例对象调用类方法

          4.类被继承后,子类也可以调用父类的类方法,当时第一个参数传入的是子类的类对象

class D(C):
    pass
D.f('子类的类对象调用父类的类方法')
#输出
#<class '__main__.D'>
#子类的类对象调用父类的类方法
posted @ 2017-12-03 17:27  土耳其大骗子  阅读(151)  评论(0)    收藏  举报