Python类中使用的“ cls”变量是什么?

问题:Python类中使用的“ cls”变量是什么?

为什么cls有时self在Python类中使用它而不是将其用作参数?

例如:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    @classmethod
    def from_fullname(cls, fullname):
        cls.firstname, cls.lastname = fullname.split(' ', 1)

Why is cls sometimes used instead of self as an argument in Python classes?

For example:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    @classmethod
    def from_fullname(cls, fullname):
        cls.firstname, cls.lastname = fullname.split(' ', 1)

回答 0

"self"和之间的区别在"cls"中定义PEP 8。正如Adrien所说,这不是强制性的。这是一种编码风格。PEP 8说:

函数和方法参数

始终使用self实例方法的第一个参数。

始终使用cls类方法的第一个参数。

The distinction between "self" and "cls" is defined in PEP 8 . As Adrien said, this is not a mandatory. It’s a coding style. PEP 8 says:

Function and method arguments:

Always use self for the first argument to instance methods.

Always use cls for the first argument to class methods.


回答 1

用于类方法的情况。检查此参考以获取更多详细信息。

编辑:正如Adrien所阐明的,这是一个约定。您实际上可以使用clsself以外的任何东西(PEP8)。

It’s used in case of a class method. Check this reference for further details.

EDIT: As clarified by Adrien, it’s a convention. You can actually use anything but cls and self are used (PEP8).


回答 2

cls表示方法属于该类,而自身表示该方法与该类的实例有关,因此with的成员cls可以通过类名访问,而with的成员可以通过该类的实例访问…这是同一概念如static membernon-static members在Java中,如果你是从Java背景。

cls implies that method belongs to the class while self implies that the method is related to instance of the class,therefore member with cls is accessed by class name where as the one with self is accessed by instance of the class…it is the same concept as static member and non-static members in java if you are from java background.


回答 3

这是一个很好的问题,但没有问题那么严重。尽管“ self”和“ cls”使用的方法位于相同的位置,但它们之间存在差异

def moon(self, moon_name):
    self.MName = moon_name

#but here cls method its use is different 

@classmethod
def moon(cls, moon_name):
    instance = cls()
    instance.MName = moon_name

现在您可以看到两者都是moon函数,但是一个可以在类内部使用,而另一个函数名称moon可以用于任何类。

对于实用的编程方法:

在设计圆类时,我们将区域方法用作cls而不是self,因为我们不希望将区域仅限于特定的圆类。

This is very good question but not as wanting as question. There is difference between ‘self’ and ‘cls’ used method though analogically they are at same place

def moon(self, moon_name):
    self.MName = moon_name

#but here cls method its use is different 

@classmethod
def moon(cls, moon_name):
    instance = cls()
    instance.MName = moon_name

Now you can see both are moon function but one can be used inside class while other function name moon can be used for any class.

For practical programming approach :

While designing circle class we use area method as cls instead of self because we don’t want area to be limited to particular class of circle only .


回答 4

类方法不接受自身参数,而是在调用方法时采用cls参数,该参数指向类(而不是对象实例)。由于类方法只能访问此cls参数,因此不能修改对象实例状态。那将需要自我。但是,类方法仍然可以修改适用于该类所有实例的类状态。

Python的技巧

Instead of accepting a self parameter, class methods take a cls parameter that points to the class—and not the object instance—when the method is called. Since the class method only has access to this cls argument, it can’t modify object instance state. That would require access to self . However, class methods can still modify class state that applies across all instances of the class.

Python Tricks