Popular style document?

Michael Chermside mcherm at destiny.com
Fri Mar 22 13:11:55 EST 2002


>> IMO the requirement to say self everywhere in methods is the worst
>> feature of python.
> 
> How come Python doesn't have an "automatic" this-variable, like C++ or
> Java?

The short answer is quite simple: that's how Guido likes it.

A much better answer would be to point out that LOTS of people like it. 
Also there are others who DISLIKE it, and probably a few who don't 
really care (although for some reason they never seem to get into these 
debates ;-)), but if you accept that there are some advantages to 
"automatic" access to instance variables like C++/Java and other 
advantages to "explicit" access like Python, then it shouldn't come as a 
surprise that there are some languages of each time. Python just happens 
to fall into the category that requires you to be explicit.

To really appreciate this, of course, you'd like to hear what some of 
the advantages of each approach are... here's my brief attempt to 
summarize a few important points:

Advantages of "Automatic" access to instance variables:
   * Less typing.
   * Somewhat more convenient to use objects (because you don't have to
     type "self." in front of instance variable accesses. Mostly about
     there being less typing.)

Advantages of "Explicit" access to instance variables
   * You never get confused about where a variable came from. A simple
     search through the text of the file will identify where it was
     defined. With "automatic" access, it could be an instance variable
     of a superclass defined elsewhere.
   * It's easy to tell instance variables apart from local variables
     (usually not a big deal, but sometimes (eg: while worrying about
     multi-threaded behavior) it can be).
   * Functions and Methods have a VERY simple relationship... methods
     are just functions that take an instance as their first parameter.
     This allows you to do things like "myObject.getFoo = myGetFooFunc",
     to create a new method.

I'm not expecting this list of advantages to convince anyone, but if you 
understand where other people are coming from, you can better understand 
why this feature/misfeature would be found in a language.

-- Michael Chermside







More information about the Python-list mailing list