Python - 自我,没有自我和cls [英] Python - self, no self and cls
问题描述
关于自我是什么的另一个问题,如果你不使用自我和什么是'cls',会发生什么。
我做了我的家庭作业,我只是想确保我得到了所有。
self - 要访问对象的属性,你需要以将对象名称(objname.attributename)作为属性名称的前缀。同样的方式self用于访问对象(类)本身内的属性。因此,如果你没有在类方法中为变量前缀self,你将无法访问该类的其他方法或该类外部的变量。所以你可以省略它,如果你想使变量局部的方法只。同样的方式,如果你有一个方法,你没有任何变量,你想与其他方法共享,你可以从方法参数中省略自我。
cls - 每个实例创建它自己的属性复制,所以如果你想让一个类的所有实例共享同一个变量,你可以在类声明中用cls作为变量名的前缀。
这是对的吗?感谢。
同样的方式self用于访问对象本身。
不在对象/类内部,只在类实例方法 self
只是一个约定,你可以调用它任何你想要的,即使不同的每个方法。
所以,如果你没有在类方法中为变量前缀self,你将无法访问该类的其他方法或该类外部的变量。在实例方法中使用
self
cls
通常用于类方法。
因此,如果您只想让变量局部化,您可以省略。
是的,在一个方法中,变量名就像任何其他函数内部一样 - 解释器在本地查找名称,然后在闭包中查找名称,然后在全局/模块级别,然后在Python内置。
同样的方法,如果你有一个方法,你没有任何变量想要与其他方法分享,你可以从方法参数中省略自我。
不,你不能忽略self 从方法参数。你必须告诉Python你需要一个 staticmethod
,它不会自动传递类的实例,通过执行 @staticmethod $在方法体下面的
def
行或 mymethod = staticmethod(mymethod)
>
每个实例创建它自己的属性复制,所以如果你想让一个类的所有实例共享同一个变量,
在类定义之内,但在任何类别之外的 方法,名称绑定到类 - 这是你如何定义方法等。你不用cls
或任何其他前缀。
cls
通常用于__ new __
specialstaticmethod
,或者在
classmethod
中,类似于staticmethod
。这些方法只需要访问类,而不是特定于类的每个实例的事物。
在
类方法
里面,是的,你可以使用它来引用你想要的类的所有实例的属性
喜欢
self
,cls $
一个简单的例子:
class Foo(object):
#你不能使用self。或cls。在这里,他们不会有什么意义
#这是一个类属性
thing ='athing'
def __init __(self,bar):
#我想要其他方法调用这个实例的Foo
#有访问bar,所以我创建一个属性self
#指向它
self.bar = bar
@staticmethod
def default_foo():
#静态方法通常用作替代构造函数
#,因为它们不需要访问类的任何部分
#如果方法根本没有任何与类相关的事情
#只使用模块级函数
return Foo('baz')
@类方法
def two_things(cls):
#可以访问类的属性,比如事情
#而不是实例属性,如bar
print cls.thing,cls.thing
Yet another question on what the 'self' is for, what happens if you don't use 'self' and what's 'cls' for. 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). The same way self is used to access an attribute inside the object (class) itself. 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. 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.
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.
Is this all right? Thanks.
解决方案The same way self is used to access an attribute inside the object (class) itself.
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.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
is used in instance methods,cls
is often used in class methods. 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.
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.
No, you can't just omit "self" from the method arguments. 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 thedef
line, ormymethod = staticmethod(mymethod)
below the method body.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.
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
is generally used in the__new__
specialstaticmethod
, or inclassmethod
s, which you make similarly tostaticmethod
s. 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.Like
self
,cls
is just a convention, and you could call it whatever you wanted.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
这篇关于Python - 自我,没有自我和cls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!