[英]Python - self, no self and cls
Yet another question on what the 'self' is for, what happens if you don't use 'self' and what's 'cls' for. 还有一个关于“自我”是什么的问题,如果你不使用'自我'和什么是'cls'会发生什么。 I "have done my homework", I just want to make sure I got it all.
我“完成了我的作业”,我只是想确保我完成了所有工作。
self
- To access an attribute of an object, you need to prefix the attribute name with the object name ( objname.attributename
). self
- 要访问对象的属性,需要在属性名称前加上对象名称( objname.attributename
)。 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为变量添加前缀,那么您将无法在类的其他方法中或类外部访问该变量。 So you could omit it if you wanted to make the variable local to that method only.
因此,如果您只想将变量局部化为该方法,则可以省略它。 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
。
cls
- 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
”作为变量名称的前缀。
Is this all right? 这样可以吗? Thanks.
谢谢。
The same way self is used to access an attribute inside the object (class) itself.
self用于访问对象(类)本身内部的属性。
Not inside the object / class, just inside the class' instance methods . 不在对象/类中,只是在类的实例方法中 。
self
is just a convention, you could call it whatever you wanted, even something different in each method. 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为变量添加前缀,那么您将无法在类的其他方法中或类外部访问该变量。
self
is used in instance methods , cls
is often used in class methods . self
在实例方法中使用 , cls
经常用在类方法中 。 Otherwise, correct. 否则,正确。
So you could omit it if you wanted to make the variable local to that method only.
因此,如果您只想将变量局部化为该方法,则可以省略它。
Yes, inside a method a variable name is like inside any other function -- the interpreter looks for the name locally, then in closures, then in the globals / module level, then in the Python built-ins. 是的,在一个方法中,变量名称就像在任何其他函数中一样 - 解释器在本地查找名称,然后在闭包中查找,然后在globals / module级别,然后在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。
No, you can't just omit "self" from the method arguments. 不,你不能只从方法参数中省略“self”。 You have to tell Python you want a
staticmethod
, which won't automatically get passed the instance of the class, ether by doing @staticmethod
above the def
line, or mymethod = staticmethod(mymethod)
below the method body. 你必须告诉Python你想要一个
staticmethod
,它不会自动传递类的实例,通过在def
行上方执行@staticmethod
,或者在方法体下面执行mymethod = staticmethod(mymethod)
。
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”作为变量名称的前缀。
Inside the class definition , but outside any methods, names are bound to the class -- that's how you define methods etc. You don't prefix them with cls
or anything else. 在类定义中 ,但在任何方法之外,名称都绑定到类 - 这就是你如何定义方法等。你不要在它们
cls
或其他任何东西。
cls
is generally used in the __new__
special staticmethod
, or in classmethod
s, which you make similarly to staticmethod
s. cls
通常用于__new__
特殊staticmethod
,或classmethod
,与staticmethod
类似。 These are methods that only need access to the class, but not to things specific to each instance of the class. 这些方法只需要访问类,但不能访问特定于每个类实例的内容。
Inside a classmethod
, yes, you'd use this to refer to attributes you wanted all instances of the class, and the class itself, to share. 在
classmethod
,是的,您可以使用它来引用您希望所有类实例和类本身共享的属性。
Like self
, cls
is just a convention, and you could call it whatever you wanted. 像
self
一样, cls
只是一种惯例,你可以cls
调用它。
A brief example: 一个简短的例子:
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
You use self
as the first argument in regular methods where the instance is passed automatically through this argument. 您可以将
self
用作常规方法中的第一个参数,其中实例通过此参数自动传递。 So whatever the first argument is in a method - it points to the current instance 因此无论第一个参数在方法中是什么 - 它指向当前实例
When a method is decorated with @classmethod
it gets the class passed as its first argument so the most common name for it is cls
as it points to the class . 当一个方法用
@classmethod
修饰时,它会将类作为第一个参数传递,因此它最常见的名称是cls
因为它指向类 。
You usually do not prefix any variables (hungarian notation is bad). 你通常不添加任何变量前缀 (匈牙利表示法是坏的)。
Here's an example: 这是一个例子:
class Test(object):
def hello(self):
print 'instance %r says hello' % self
@classmethod
def greet(cls):
print 'class %r greet you' % cls
Output: 输出:
>>> Test().hello()
instance <__main__.Test object at 0x1f19650> says hello
>>> Test.greet()
class <class '__main__.Test'> greet you
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.