Why self?

Jesper Olsen jolsen at mailme.dk
Fri Jul 5 14:17:11 EDT 2002


Thanks for the reference.

Actually I subscribe to the "explicit is better than implicit"  axiom
also.

What I mean by the legacy part is that you have to name self in the
list of
arguments to the class methods.
This part is redundant - but once you are inside a method, it is fine
that
you have to explicitly qualify by self when refering to class
variables.

As holger points out, a frequent source of errors in C++ is that a
method takes an argument with the same name as a class variable -
which then hides the class variable.

When I prototype new classes, the methods often start out as
functions, and typically I forget to add self to the argument list
when they are promoted to methods.

It is not a big thing and not difficult to spot when it happens, but
still it bugs me, and I think the language would be better off without
it.

Of course changing this now would brake the existing code base, so I
guess we will have to stick with it. Legacy.

Cheers
Jesper


> > So, is it just legacy or is there a reason for this convention?
> 
>   http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.007.htp



More information about the Python-list mailing list