Python classmethod装饰器原理及用法解析
英文文档:classmethodReturn a class method for function.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:class C:It can be called either on the class or on an instance . 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. 标记方法为类方法的装饰器说明: 1. classmethod 是一个装饰器函数,用来标示一个方法为类方法 2. 类方法的第一个参数是类对象参数,在方法被调用的时候自动将类对象传入,参数名称约定为cls 3. 如果一个方法被标示为类方法,则该方法可被类对象调用,也可以被类的实例对象调用 4. 类被继承后,子类也可以调用父类的类方法,但是第一个参数传入的是子类的类对象
用户评论