Do I need "self" and "other"?

Kurda Yon kurdayon at yahoo.com
Sat Jun 28 00:41:22 CEST 2008


On Jun 27, 6:32 pm, Hans Nowak <zephyrfalcon!NO_SP... at gmail.com>
wrote:
> 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 = []
> >     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?
>
> You can use whichever (valid) names you want, but in general 'self' and 'other'
> are used for clarity.  In this case, they indicate the vector that is operated
> on ("self") and another vector ("other").  Using 'x' and 'y' would be less clear
> here.
>
> --
> Hans Nowak (zephyrfalcon at gmail dot com)http://4.flowsnake.org/

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.



More information about the Python-list mailing list