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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4613000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:31:19  来源:igfitidea点击:

What is the 'cls' variable used for in Python classes?

pythonclassobjectterminologyself

提问by Scaraffe

Why is clssometimes used instead of selfas an argument in Python classes?

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

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)

回答by Juho Veps?l?inen

It's used in case of a class method. Check this referencefor further details.

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

EDIT: As clarified by Adrien, it's a convention. You can actually use anything but clsand selfare used (PEP8).

编辑:正如阿德里安澄清的那样,这是一个约定。实际上,你可以使用任何东西,但clsself使用(PEP8)。

回答by Baskaya

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 8says:

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

Function and method arguments:

Always use selffor the first argument to instance methods.

Always use clsfor the first argument to class methods.

函数和方法参数

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

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

回答by TheExorcist

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

这是一个很好的问题,但没有问题那么迫切。'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

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.

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

For practical programming approach :

对于实际的编程方法:

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

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

回答by abdilatif bashir

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

cls暗示方法属于类,而 self 暗示方法与类的实例相关,因此成员 withcls是通过类名访问的,而 self 是由类的实例访问的......这是相同的概念如果您来自 java 背景,则static membernon-static members在 java 中一样。

回答by authentichigh

Instead of accepting a self parameter, class methods take a clsparameter 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.

不是接受自参数,类方法采取CLS参数指向-and不是对象实例-当该方法被调用。由于类方法只能访问此 cls 参数,因此无法修改对象实例 state。那将需要访问 self 。但是,类方法仍然可以修改适用于该类所有实例的类状态。

-Python Tricks

- Python 技巧