[Tutor] Classes, methods and self
Alan Gauld
alan.gauld@blueyonder.co.uk
Fri Aug 1 15:46:02 2003
> and Learning Python) and I cannot seem to understand why self is
needed
> in a method declaration. I "believe" that I understand what self
does
> (i.e. connects itself to the current object instance).
Corrct.
> However, what I do not understand is why you have to explicitly
> declare self in the argument list of a method.
Because that's how Guido van Rossum designed the Python language.
Other languages (eg. Java, C++) let the compiler/interpreter do it
for you. However there are some avantages to explicit self, not
least because it helps make the fact that you are writing a method,
as opposed to a function, more obvious.
Using self inside the methods is also not needed in other languages
but again there are advantages to the Python approach in that it
becomes clear which variables are local to the method and which
are part of the object(and thus persist beyond the method execution)
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld