[Tutor] problem with class - get message that self is not defined

Alan Gauld alan.gauld at freenet.co.uk
Tue May 23 23:17:22 CEST 2006


>> self is a special varianble
> "special variable" could be confusing. Python has no "special 
> variables". "self" is used by convention.

Good point. The name self is not special it is its role that is 
special.

thus

class C:
    def m(s,p):
        print p

c = C()
c.m(42)

Thus we define method m to have two parameters s and p
but when we call m we only pass in one argument. Python
then takes this and turns it into

C.m(c,42)

The specialness of 'self' is that although we specify it, and can 
access it,
in the method definition we do not specify it in the message to the 
class
that invokes the method.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list