Python中"self"和"__init__"表达式的含义是什么? [英] What is the meaning of 'self' and '__init__' expressions in Python?

查看:129
本文介绍了Python中"self"和"__init__"表达式的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白它们的用途,特别是self参数?有人可以向我解释一下,为什么在地球上您想将其传递给我?

I don't understand what these are used for, particularly the self argument? Could some please explain this to me and why on earth you would want to pass this in?

此外,我一直认为__init__是用于初始化"的,但是我从没想到我以前从未在这里放置过任何东西.有人可以给我举个简单的例子吗?

Also, I've always thought __init__ was for 'initialisation', but it didn't occur to me that I've never had to put anything in here before. Could someone give me an easy example?

每当看到self被传递到函数之类的东西时,我都会感到非常困惑.

edit: i just get so confused everytime i see self being passed into a function, or something of the like.

推荐答案

self是您要在其上调用方法的对象.有点像Java中的this.

self is the object you're calling the method on. It's a bit like this in Java.

__init__.就像Java中的构造函数.

__init__ is called on each object when it is created to initialise it. It's like the constructor in Java.

因此,只要要在创建对象时设置对象的任何属性(Java中的成员变量),就可以使用__init__.如果您对空"感到满意,对象,您不需要__init__方法,但是如果要创建带有参数的对象,则需要一个.

So you would use __init__ whenever you wanted to set any attributes - member variables in Java - of the object when it was created. If you're happy with an "empty" object you don't need an __init__ method but if you want to create an object with arguments you'll need one.

一个例子是:

class StackOverflowUser:
    def __init__(self, name, userid, rep): 
        self.name = name
        self.userid = userid
        self.rep = rep

dave = StackOverflowUser("Dave Webb",3171,500)

然后我们可以查看我们创建的对象:

We can then look at the object we've created:

>>> dave.rep
500
>>> dave.name
'Dave Webb'

因此我们可以看到__init__传递给了我们提供给构造函数的参数以及self,该参数是对已创建对象的引用.然后在处理参数并适当更新对象时使用self.

So we can see __init__ is passed the arguments we gave to the constructor along with self, which is the reference to the object that has been created. We then use self when we process the arguments and update the object appropriately.

有一个问题,为什么当其他语言不需要Python时,为什么Python具有self.根据

There is the question of why Python has self when other languages don't need it. According to the Python FAQ:

为什么必须在方法定义和调用中显式使用'self'?

首先,很明显,您使用的是方法或实例属性而不是局部变量...

First, it's more obvious that you are using a method or instance attribute instead of a local variable...

第二,这意味着如果要显式引用或从特定类调用方法,则不需要特殊语法.

Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class...

最后,例如,它解决了赋值的语法问题:由于Python中的局部变量(按定义!)是那些在函数体中分配了值的变量(并且未明确声明为全局),因此必须以某种方式告诉解释器,分配是要分配给实例变量而不是局部变量,并且它最好是语法上的(出于效率考虑)...

Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value assigned in a function body (and that aren't explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons)...

这篇关于Python中"self"和"__init__"表达式的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆