Simple questions about classes

Rene Pijlman reageer.in at de.nieuwsgroep
Tue Feb 11 16:02:22 EST 2003


Natan:
>Hi, i`m a newbie in python, and i was reading the tutorial (and some
>source codes), then i noticed that every function inside a class
>receives the 'self' parameter.
[...]
>I think self points to the class, but why do we need to pass this
>parameter to every function?

This is a FAQ. You know the FAQ, don't you? I'm also a newbie,
but I've read it and that makes the difference :-)

"6.7. Why must 'self' be declared and used explicitly in method
definitions and calls?

When classes were added to Python, this was (again) the simplest
way of implementing methods without too many changes to the
interpreter. The idea was borrowed 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. 
[...]
Second, it means that no special syntax is necessary if you want
to explicitly reference or call the method from a particular
class."
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.007.htp

-- 
René Pijlman

Wat wil jij leren?  http://www.leren.nl




More information about the Python-list mailing list