Why self?

Lou Pecora pecora at anvil.nrl.navy.mil
Mon Jul 8 14:58:06 EDT 2002


In article <3D29CE9D.4000802 at onsitetech.com>, Robb Shecter
<rs at onsitetech.com> wrote:

> I completely agree, but I think the situation's even worse than you 
> described:  A'typical' case of standard OO programming is made the most 
> tedious to write:  References to instance variables from methods:
> 
>      self.__velocity = self.__factor1 * self.__factor2
> 
> etc.  I find this totally unreadable (7 extra characters per referenced 
> instance variable).

You know, as much as I LOVE Python I got to agree this one bugs me,
too.  I wish the scoping rules could be changed to default to self
first, then local and the rest of the order _without_ the "self." part.

Solution? Sometimes I just use the underscore "_" as the self object
reference.  That shortens the name.  You don't have to use the word
"self."  viz.,

# Within the object definition, x and z are object variables ...

     def func(_):
         y=_.x * _.z
         return y

Not the best solution, but saves typing.



More information about the Python-list mailing list