Instance methods vs. functions and the self parameter
Henrik Hansen
henrik-hansen at vip.cybercity.dk
Sun Feb 2 08:49:15 EST 2003
I am learning Python using the book "Practical Python", and have a
little trouble understanding this example (I am familiar with OO
concepts, having some experience with Java):
<example>
>>> class Class:
def method(self):
print "I have a self!"
>>> def function():
print "I don't..."
>>> instance = Class()
>>> instance.method()
I have a self!
>>> instance.method = function
>>> instance.method()
I don't...
</example>
Ok, so I understand that you can assign instance.method to function, but
what about the self argument? As I understand it, when calling a method
like this: foo.bar() would be equivalent to calling like this (assuming
that foo is an instance of class Baz): Baz.bar(foo). Is that correct?
In other words, why doesn't the function need to be declared
function(self): ..., since it receives a parameter?
I must be wrong in some of my assumptions, some guidance would be
appreciated.
--
Henrik Hansen
More information about the Python-list
mailing list