class objects, method objects, function objects

Duncan Booth duncan.booth at invalid.invalid
Mon Mar 19 16:23:05 EDT 2007


"7stud" <bbxx789_05ss at yahoo.com> wrote:

> When the method object is called with an
> argument list,
> 
> x.g("GvR") ==> I think I'm both "referencing an instance attribute"
> and calling the method object with an argument list
> 
> it(the method object) is unpacked again, a new argument list is
> constructed from the instance object and the original argument list,
> and the function object is called with this new argument list.

It may help you to understand if you consider the two parts separately. 
There is no linkage between 'referencing an instance attribute' and 
'calling the method object'. You can rewrite the code as:

    method = x.g
    method("GvR")

and it works in exactly the same way. First you reference the attribute 
'x.g', which looks up MyClass.g (an unbound method) and calls 
MyClass.g.__get__(x, MyClass) which creates a new bound method. Then you 
call the bound method passing "GvR" as the first positional argument: so a 
single element tuple is created for the argument list.

The bound method object unpacks and repacks the tuple to give the two-tuple 
(self, "GvR") which it then passes to the original function.



More information about the Python-list mailing list