Attack a sacred Python Cow

Michael Torrie torriem at gmail.com
Mon Jul 28 01:37:48 EDT 2008


Colin J. Williams wrote:
>>
>>     def fun( ., cat):
>>
> I don't see the need for the comma in fun.

It (the entire first variable!) is needed because a method object is
constructed from a normal function object:

def method(self,a,b):
	pass

class MyClass(object):
	pass

MyClass.testmethod=method

That's precisely the same as if you'd defined method inside of the class
to begin with.  A function becomes a method when the lookup procedure in
the instance object looks up the attribute and returns (from what I
understand) essentially a closure that binds the instance to the first
variable of the function.  The result is known as a bound method, which
is a callable object:

>>> instance=MyClass()

>>> instance.testmethod
<bound method MyClass.testmethod of <__main__.instance object at xxx>>


How would this work if there was not first parameter at all?

In short, unlike what most of the implicit self advocates are saying,
it's not just a simple change to the python parser to do this.  It would
require a change in the interpreter itself and how it deals with classes.



More information about the Python-list mailing list