Confused with methods

Matthias Kempka lists at mkempka.de
Sun Feb 6 11:19:52 EST 2005


Hi Gerald,

When you define an instance method, the first parameter (in the 
definition) represents the instance. By convention, you would name it 
"self":

#####################
class B:
    def foo(self, x):
       print "we have two parameters: " + str(self) + " and " + x
#####################

then calling

######################
b = B()
b.foo("x")
######################

would be equivalent to

######################
b = B()
B.foo(b, "x")
######################


So, as you have noted, you need at least one parameter to attach the 
method to an instance. This is because the instance _is_ the parameter. 
Python does this for you internally.

For more documentation you should read the paragraph about classes in 
the tutorial.

Regards,
Matthias



More information about the Python-list mailing list