Re: thesis-miale.ps

Guido.van.Rossum@cwi.nl
Wed, 27 Jul 1994 18:55:56 +0200

> I have just read the thesis-miale.ps file from cwi, I was wondering
> what the status was of the changes made to allow access protection
> on data and methods in classes, and the elimination of the self
> parameter to each method ?
>
> Have they been rejected ? Or just not implemented yet due to subtle
> conflicts with the language ?

The access statement has actually been implemented, although not
exactly according to Steve's proposal. I am not entirely happy with
it yet, and therefore haven't documented or publicized it, but it
works. For example, the standard module aifc (Lib/aifc.py) uses it to
protect its internal methods and variables.

Eliminating "self" from methods has been discussed on the Python
mailing list (long ago) and the conclusion was that it was better to
leave it in. See also Q. 6.9. in the FAQ:

6.9. Q. Why must 'self' be declared and used explicitly in method
definitions and calls?

A. By asking this question you reveal your C++ background. :-)
When I added classes, this was (again) the simplest way of
implementing methods without too many changes to the interpreter. I
borrowed the idea from Modula-3. It turns out to be very useful, for
a variety of reasons.

First, it makes it more obvious that you are using a method or
instance attribute instead of a local variable. Reading "self.x" or
"self.meth()" makes it absolutely clear that an instance variable or
method is used even if you don't know the class definition by heart.
In C++, you can sort of tell by the lack of a local variable
declaration (assuming globals are rare or reasily recognizable) -- but
in Python, there are no local variable declarations, so you'd have to
look up the class definition to be sure.

Second, it means that no special syntax is necessary if you want to
explicitly reference or call the method from a particular class. In
C++, if you want to use a method from base class that is overridden in
a derived class, you have to use the :: operator -- in Python you can
write baseclass.methodname(self, <argument list>). This is
particularly useful for __init__() methods, and in general in cases
where a derived class method wants to extend the base class method of
the same name and thus has to call the base class method somehow.

Lastly, for instance variables, it solves a syntactic problem with
assignment: since local variables in Python are (by definition!) those
variables to which a value assigned in a function body (and that
aren't explicitly declared global), there has to be some way to tell
the interpreter that an assignment was meant to assign to an instance
variable instead of to a local variable, and it should preferably be
syntactic (for efficiency reasons). C++ does this through
declarations, but Python doesn't have declarations and it would be a
pity having to introduce them just for this purpose. Using the
explicit "self.var" solves this nicely. Similarly, for using instance
variables, having to write "self.var" means that references to
unqualified names inside a method don't have to search the instance's
directories.

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>