关于类:Python – 自我,没有自我和cls

Python - self, no self and cls

还有一个关于"自我"的问题,如果你不使用"自我"会发生什么,以及"cls"是什么。我"已经完成了我的家庭作业",我只想确保我把它都做好。

self—要访问对象的属性,需要在属性名前面加上对象名(objname.attributename)。与使用self访问对象(类)本身内部属性的方式相同。因此,如果在类方法中没有给变量加上self前缀,就无法在类的其他方法中或类外部访问该变量。因此,如果只想使变量成为该方法的局部变量,可以省略它。同样地,如果您有一个方法,并且您没有想要与其他方法共享的任何变量,那么可以从方法参数中省略self。

cls—每个实例都创建自己的属性"副本",因此,如果希望类的所有实例共享同一个变量,则可以在类声明中为该变量名加前缀"cls"。

这样可以吗?谢谢。


The same way self is used to access an attribute inside the object (class) itself.

号

不在对象/类内,只在类的实例方法内。self只是一个约定,您可以随意调用它,甚至在每个方法中都有不同的内容。

So if you didn't prefix a variable with self in a class method, you wouldn't be able to access that variable in other methods of the class, or outside of the class.

号

实例方法中使用self,类方法中经常使用cls。否则,请更正。

So you could omit it if you wanted to make the variable local to that method only.

号

是的,在一个方法中,变量名与其他任何函数中的变量名一样——解释器在本地查找该名称,然后在闭包中查找,然后在全局/模块级别查找,最后在python内置模块中查找。

The same way if you had a method and you didn't have any variable you wanted to share with other methods, you could omit the self from the method arguments.

号

不,不能从方法参数中省略"self"。你必须告诉python你想要一个staticmethod,它不会自动通过类的实例,通过在def行的上面执行@staticmethod,或者在方法体的下面执行mymethod = staticmethod(mymethod),来传递ether。

Each instance creates it's own"copy" of the attributes, so if you wanted all the instances of a class to share the same variable, you would prefix that variable name with 'cls' in the class declaration.

号

在类定义的内部,但在任何方法的外部,名称都绑定到类——这就是定义方法等的方式。您不会在它们前面加上cls或其他任何东西。

cls一般用在__new__专用staticmethod中,或用在classmethod中,这与staticmethod中的做法类似。这些方法只需要访问类,而不需要访问类的每个实例的特定内容。

在classmethod中,是的,您可以使用它来引用希望类的所有实例以及类本身共享的属性。

和self一样,cls只是一种约定,你可以随意称呼它。

一个简单的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Foo(object):

    # you couldn't use self. or cls. out here, they wouldn't mean anything

    # this is a class attribute
    thing = 'athing'

    def __init__(self, bar):
        # I want other methods called on this instance of Foo
        # to have access to bar, so I create an attribute of self
        # pointing to it
        self.bar = bar

    @staticmethod
    def default_foo():
        # static methods are often used as alternate constructors,
        # since they don't need access to any part of the class
        # if the method doesn't have anything at all to do with the class
        # just use a module level function
        return Foo('baz')

    @classmethod
    def two_things(cls):
        # can access class attributes, like thing
        # but not instance attributes, like bar
        print cls.thing, cls.thing


在常规方法中,使用self作为第一个参数,其中实例自动通过此参数传递。所以无论方法中的第一个参数是什么,它都指向当前实例

当用@classmethod修饰一个方法时,它得到作为第一个参数传递的类,因此它最常用的名称是cls,因为它指向该类。

通常不给任何变量加前缀(匈牙利符号不好)。

下面是一个例子:

1
2
3
4
5
6
class Test(object):
    def hello(self):
        print 'instance %r says hello' % self
    @classmethod
    def greet(cls):
        print 'class %r greet you' % cls

输出:

1
2
3
4
5
>>> Test().hello()
instance <__main__.Test object at 0x1f19650> says hello

>>> Test.greet()
class <class '__main__.Test'> greet you

号