Simple questions about classes

marshall marshall at spamhole.com
Tue Feb 11 21:47:44 EST 2003


nvivo at terra.com.br (Natan) wrote in message news:<bbb4a9e1.0302111148.48dc074b at posting.google.com>...
> 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.
> 
> Like this exemple in python tutorial:
> 
> ---
> class MyClass:
>     "A simple example class"
>     i = 12345
>     def f(self):
>         return 'hello world'
> ---
> 
> I think self points to the class, but why do we need to pass this
> parameter to every function?

'self' refers to the instance of the class (object) whose method is
being invoked.  It has to get passed to the object when calling the
method so that the method knows what object it is working on (in?). 
I'm not sure that is 100% correct but that is how I visualize it. (I
also visualize all my objects wearing nametags that say 'self', upside
down so they can read them of course.)

One thing that you may have already noticed is that 'self' is
automatically passed when the method is called.




More information about the Python-list mailing list