understanding self

bruno modulix onurb at xiludom.gro
Thu Jul 8 18:36:05 EDT 2004


bruce stockwell a écrit :
> Using 'self' in classes seems pretty straight forward. My curiosity is 
> why I have to use it. Shouldn't it be implied? If I create an instance 
> of 'human' called 'bruce' and call the method 'blink' why do I have to 
> pass bruce into the method e.g.
> 
(snip)

> blink is a method of bruce because bruce is an instance of human. 
> Reading this silly code I can understand what is going on if self was 
> not included. Can anyone enlighten me as to why we have self?

You already got some good answers. I'll just add my two cents :

class Test:
   pass

t = Test()

def fun(self, arg):
   self.arg = arg
   print self.arg

Test.fun = fun

t.fun(42)
 >>> 42
print t.arg
>>> 42

Could you do this without self ?

> by the way..six months of python and I'm 10 times the programmer I was 
> with vb

Enjoy !-)

Bruno



More information about the Python-list mailing list