zoukankan      html  css  js  c++  java
  • Python @classmethod

    @classmethod:

    定义操作类,而不是操作实例的方法。classmethod改变了调用方法的方式,因此类方法的第一个参数是类本身,而不是实例。@classmethod最常见的用途是定义备选构造方法。

    1.定义方式

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式
    • 普通的self类方法foo()需要通过self参数隐式的传递当前类对象的实例

    • @classmethod修饰的方法class_foo()需要通过cls参数传递当前类对象

    • @staticmethod修饰的方法定义与普通函数是一样的。

    self和cls的区别不是强制的,只是PEP8中一种编程风格,self通常用作实例方法的第一参数,cls通常用作类方法的第一参数。即通常用self来传递当前类对象的实例,cls传递当前类对象。

    2. @classmethod 特点

    • 声明一个类方法
    • 第一个参数必须是 cls, 它可以访问类的属性
    • 类的方法只能访问类的属性,不能访问实例属性
    • 类的方法可以通过 ClassName.MethodName() 访问也可以通过实例来访问。
    • 它可以返回一个类的实例

    例子:

    class Student:
        name = 'unknown' # class attribute
        def __init__(self):
            self.age = 20  # instance attribute
    
        @classmethod
        def tostring(cls):
            print('Student Class Attributes: name=',cls.name)
    

    类方法访问实例属性时,会报错

    class Student:
        name = 'unknown' # class attribute
        def __init__(self):
            self.age = 20  # instance attribute
    
        @classmethod
        def tostring(cls):
            print('Student Class Attributes: name=',cls.name,', age=', cls.age)
    
    >>> Student.tostring()
    Traceback (most recent call last):
      File "<pyshell#22>", line 1, in <module>
        Student.tostring()
      File "<pyshell#21>", line 7, in display
        print('Student Class Attributes: name=',cls.name,', age=', cls.age)
    AttributeError: type object 'Student' has no attribute 'age'
    

    例子

    from datetime import date
    
    # random Person
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        @staticmethod
        def fromFathersAge(name, fatherAge, fatherPersonAgeDiff):
            return Person(name, date.today().year - fatherAge + fatherPersonAgeDiff)
    
        @classmethod
        def fromBirthYear(cls, name, birthYear):
            return cls(name, date.today().year - birthYear)
    
        def display(self):
            print(self.name + "'s age is: " + str(self.age))
    
    class Man(Person):
        sex = 'Male'
    
    man = Man.fromBirthYear('John', 1985)
    print(isinstance(man, Man))
    
    man1 = Man.fromFathersAge('John', 1965, 20)
    print(isinstance(man1, Man))
    

    3. @classmethod 和 @staticmethod

    @classmethod @staticmethod
    声明一个类的方法 声明一个静态方法
    可以访问类的属性,不可以访问实例属性 既不可以访问类属性,也不可以访问实例属性
    可以使用 ClassName.MethodName() 调用或者 object.MethodName().调用 可以使用 ClassName.MethodName() 调用后者object.MethodName().调用
    可以声明一个类的工厂方法,返回一个实例 不可以返回一个实例

    参考:

    https://www.programiz.com/python-programming/methods/built-in/classmethod
    https://www.tutorialsteacher.com/python/classmethod-decorator

    不要小瞧女程序员
  • 相关阅读:
    No-3.Linux 终端命令格式
    No-2.常用 Linux 命令的基本使用
    No-1.文件和目录
    No-7.运算符
    No-6.If语句
    No-5.变量的命名
    YOLOv4详细分析 | 细数当前最佳检测框架小细节(附论文及源码下载)
    案例】S7-200SMART 实时时钟如何在MCGS触摸屏上显示并写入
    卡尔曼滤波:从入门到精通
    mmdetection最小复刻版(七):anchor-base和anchor-free差异分析
  • 原文地址:https://www.cnblogs.com/shix0909/p/15037612.html
Copyright © 2011-2022 走看看