Confused with methods

Diez B. Roggisch deetsNOSPAM at web.de
Sun Feb 6 16:04:28 EST 2005


> I expected that when we add this "x" to a class's dictionary and
> then we request it from an instance of that class, it will be
> converted to an bound-method and receive its --one-- argument
> from the referring instance.

Here you are wrong: your b.foo is a bound method - it already _has_ its
first argument (an instance of B) bound to it. And just passing it around
doesn't change that. You can assign it to a name in whatever scope you like
- that won't change its nature.

Now if you want to use foo in A as instancemethod, you could do this:

A.foo = b.foo.im_func
a = A()
a.foo(200)


That works because im_func is a "pure" function:

>>> B.foo.im_func
<function foo at 0xb7ac47d4>


If things worked as you wanted it to, that would mean that passing a bound
method as argument to a class and storing it there to a instance variable
that would "eat up" the arguments - surely not the desired behaviour.
-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list