Why self?

Robb Shecter rs at onsitetech.com
Tue Jul 9 15:19:40 EDT 2002


Charles Hixson wrote:

> If you are subclassing a class that someone else has written, you don't 
> want to accidentally redefine an internal name.  Sorry.  I can accept 
> that you should be able to explicitly access it, but to be able to 
> change it by accident is (would be?) a misfeature.
> 
> I didn't believe that this happened in Python.  It does, and it seems a 
> significant flaw....

Yeah - this is getting to what I'm talking about.  From the Python 
Tutorial, section 9.6:

   "Name mangling is intended to give classes an easy way to define
   ``private'' instance variables and methods, without having to worry
   about instance variables defined by derived classes [...]. Note that
   the mangling rules are designed mostly to avoid accidents..."

This is where I got the idea that variables should be marked with __ in 
order to function in the way you expect.  Like, if I understand this 
correctly, a programmer could be forced to comb recursively through the 
code of all superclasses looking for possible collisions.

Robb




More information about the Python-list mailing list