how to give an object as argument for a method

Geoff Gerrietts geoff at gerrietts.net
Thu Mar 7 14:50:39 EST 2002


Quoting Marco Herrn (herrn at gmx.net):
> Is there a reason why this is just a convention? It seems (because
> it is such a _strong_ convention) that this should be a real feature
> of the language. I mean that there could be a reserved word "self"
> which is there all the time, so that one doesn't hast to do this
> himself.

Others have posted some very good reasons why this isn't so, but
consider the advantages that explicitly passing "self" as a first
argument has:

>>> def myFunction(self, spam, eggs):
...     print "Self:", self
...     print "spam:", spam
...     print "eggs:", eggs
... 
>>> class MyClass:
...     myMethod = myFunction
... 
>>> my_instance = MyClass()
>>> my_instance.myMethod('Yellow', 'Green')
Self: <__main__.MyClass instance at 0x814f3a4>
spam: Yellow
eggs: Green

Is that not cool? You know it is.

-- 
Geoff Gerrietts                <geoff at gerrietts net> 
"I have read your book and much like it." --Moses Hadas




More information about the Python-list mailing list