Do I need "self" and "other"?

Roy Smith roy at panix.com
Sat Jun 28 04:15:21 CEST 2008


In article 
<68566b52-100d-40ee-a0c6-bde20df9ecd4 at a70g2000hsh.googlegroups.com>,
 Kurda Yon <kurdayon at yahoo.com> 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?

Technically, Python doesn't care what you call them.  You could write:

def __add__(luxury_yacht, throat_warbler_mangrove):
   etc, etc, etc

and it would run exactly the same.  That being said, don't do that.  The 
use of "self" and "other" are pretty much de rigueur.  Experienced Python 
programers will instantly understand what they mean.  If you use anything 
else, they will have to spend more time trying to understand your code.



More information about the Python-list mailing list