我需要“自我”吗?和“其他”? [英] Do I need "self" and "other"?

查看:106
本文介绍了我需要“自我”吗?和“其他”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我找到了一个例子,它定义了两个向量的添加,作为类的一个b / b $ b方法。它看起来像是:


类Vector:

def __add __(self,other):

data = []

为范围内的j(len(self.data)):

data.append(self.data [j] + other.data [j])

返回向量(数据)


在此示例中,使用self和其他。真的需要

使用这个词吗?如果有,为什么?我已经取代了自我通过x表示和

其他通过y一切看起来都不错。它真的没问题,或者我可以在某些情况下有一些问题吗?


谢谢!

Hi,

I found one example which defines the addition of two vectors as a
method of a class. It looks like that:

class Vector:
def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)

In this example one uses "self" and "other". Does one really need to
use this words? And, if yes, why? I have replaced "self" by "x" and
"other" by "y" and everything looks OK. Is it really OK or I can have
some problem in some cases?

Thank you!

推荐答案

Kurda Yon写道:
Kurda Yon wrote:




我找到了一个定义添加的例子两个向量作为一个类的

方法。它看起来像是:


类Vector:

def __add __(self,other):

data = []

为范围内的j(len(self.data)):

data.append(self.data [j] + other.data [j])

返回向量(数据)


在此示例中,使用self和其他。真的需要

使用这个词吗?如果有,为什么?我已经取代了自我通过x表示和

其他通过y一切看起来都不错。它确实没问题,或者我可以在某些情况下有一些问题吗?
Hi,

I found one example which defines the addition of two vectors as a
method of a class. It looks like that:

class Vector:
def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)

In this example one uses "self" and "other". Does one really need to
use this words? And, if yes, why? I have replaced "self" by "x" and
"other" by "y" and everything looks OK. Is it really OK or I can have
some problem in some cases?



你可以使用你想要的任何(有效)名字,但一般来说''self''和''other''

是用于清晰。在这种情况下,它们表示在(self)上操作的矢量和另一个矢量(other)。使用''x''和''y''不太清楚

这里。


-

Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/


6月27日下午6:32,Hans Nowak< zephyrfalcon!NO_SP ... @ gmail.com>

写道:
On Jun 27, 6:32 pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
wrote:

Kurda Yon写道:
Kurda Yon wrote:


Hi,


我找到了一个例子,它定义了两个向量的加法作为一个类的

方法。看起来像这样:
I found one example which defines the addition of two vectors as a
method of a class. It looks like that:


class Vector:

def __add __(self,other):

data = []

为范围内的j(len(self.data)):

data.append(self.data [j] + other.data [ j])

返回向量(数据)
class Vector:
def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)


在此示例中,使用self和其他。真的需要

使用这个词吗?如果有,为什么?我已经取代了自我通过x表示和

其他通过y一切看起来都不错。它确实没问题,或者我可以在某些情况下有一些问题吗?
In this example one uses "self" and "other". Does one really need to
use this words? And, if yes, why? I have replaced "self" by "x" and
"other" by "y" and everything looks OK. Is it really OK or I can have
some problem in some cases?



你可以使用你想要的任何(有效)名字,但一般来说''self''和''other''

是用于清晰。在这种情况下,它们表示在(self)上操作的矢量和另一个矢量(other)。使用''x''和''y''不太清楚

这里。


-

Hans Nowak (zephyrfalcon at gmail dot com) http://4.flowsnake.org/



好​​的,我明白了。在给出的示例中,self表示self。只是一个名称,可以是

替换为任何(有效)名称。这总是那样吗?我的意思是,

确实是slef在某些情况下有特殊意义,或者它总是只是

一个像其他任何名字一样的名字?我问的是因为自我在我的文本编辑器中突出显示

,所以我认为它具有特殊含义。我还听说过自我。指的是一个物体而我不确定它是指什么?b $ b"指的是意味着。

OK, I see. In the given example "self" is just a name which can be
replace by whichever (valid) name. Is that always like that? I mean,
does "slef" have a special meaning in some cases or it is always "just
a name like any other"? I am asking that because "self" is highlighted
in my text editor, so I assume that it can have a special meaning. I
also heard that "self" refers to a object and I am not sure what that
"refers" means.





Kurda Yon写道:


Kurda Yon wrote:

> ;


好​​的,我明白了。在给出的示例中,self表示self。只是一个名称,可以是

替换为任何(有效)名称。这总是那样吗?我的意思是,

确实是slef在某些情况下有特殊意义,或者它总是只是

一个像其他任何名字一样的名字?
>

OK, I see. In the given example "self" is just a name which can be
replace by whichever (valid) name. Is that always like that? I mean,
does "slef" have a special meaning in some cases or it is always "just
a name like any other"?



是。


方法是绑定到类或实例的函数。

Def语句创建函数。参数名称是任意的,只要

,因为它们不会与您想要从函数中的
访问的任何全局名称冲突。


自我(和其他)只是社区惯例。如果它们仅用作函数/方法参数名称,那么它们确实具有它们的优势,那么它们不会与任何模块全局变量冲突。


tjr

Yes.

A method is a function bound to a class or instance thereof.
Def statements create functions. Parameter names are arbitrary, as long
as they do not conflict with any global names you want to access from
within the function.

Self (and other) are simply community conventions. They do have they
advantage that if they are only used as function/method parameter names,
then they will not conflict with any module globals.

tjr


这篇关于我需要“自我”吗?和“其他”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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