在python中使用@classmethod有什么用以及何时使用?

Fra*_*sco 4 python decorator python-decorators

我从来没有使用过@classmethod,也没有想到任何使用它的例子,我知道它是如何工作的,但我不知道什么时候使用它

class Example:
    def __init__(self,param1,param2):
        self.param1 = param1
        self.param2 = param2
    @classmethod
    def my_method(cls,param1,param2):
        return cls(param1,param2)

example = Example.my_method(1,2)
print(example)
Run Code Online (Sandbox Code Playgroud)

输出:

<__main__.Example object at 0x02EC57D0>
Run Code Online (Sandbox Code Playgroud)

但为什么不这样做呢?

class Example:
    def __init__(self,param1,param2):
        self.param1 = param1
        self.param2 = param2

    def my_method(self,param1,param2):
        return Example(param1,param2)

example = Example(1,2)
method = example.my_method(3,4)
print(method)
Run Code Online (Sandbox Code Playgroud)

输出:

<__main__.Example object at 0x02EC57D0>
Run Code Online (Sandbox Code Playgroud)

这是相同的结果,但是当我可以使用 classmethod 时它不会出现在我的脑海中

Nav*_*eng 5

python中有3种方法:

  • 实例方法
  • 类方法
  • 静态方法
class Person():
    species='homo_sapiens' # This is class variable
    def __init__(self, name, age):
        self.name = name # This is instance variable
        self.age = age

    def show(self):
        print('Name: {}, age: {}.'.format(self.name, date.today().year - self.age))

    @classmethod
    def create_with_birth_year(cls, name, birth_year):
        return cls(name, date.today().year - birth_year)

    @classmethod
    def print_species(cls):
        print('species: {}'.format(cls.species))

    @staticmethod
    def get_birth_year(age):
        return date.today().year - age


class Teacher(Person):
    pass

Run Code Online (Sandbox Code Playgroud)

1)实例方法(show)需要一个实例并且必须使用self作为第一个参数。它可以通过self访问实例并影响实例的状态。

2)类方法(create_with_birth_yearprint_species)需要没有实例和使用CLS访问类,并影响一个类的状态。我们可以使用@classmethod来做一个工厂,比如:

navy = Person.create_with_birth_year('Navy Cheng', 1989)
navy.show()
Run Code Online (Sandbox Code Playgroud)

这家工厂可以继承

zhang = Teacher.create_with_birth_year('zhang', 1980)
print(type(zhang))
Run Code Online (Sandbox Code Playgroud)

和类方法可以用来访问类变量:

Person.print_species()
Run Code Online (Sandbox Code Playgroud)

3)静态方法(get_birth_year)不需要特殊参数(selfcls)并且会改变类或实例的任何状态。它可以为一个类提供一些辅助函数。