Tuesday, May 31, 2005

How well do you know Python, part 7

Given the following two classes,

class A:
    def foo(self):
        print self.__class__.__name__

class B:

why does this work,

def bar(self):
    print self.__class__.__name__

B.foo = bar
b = B()

while this does not?

B.foo = A.foo
b = B()


mark said...

the second one is like exactly the same as calling


calling the instance method on the class not an instance.

what of course does work is:

b.foo = a.foo

because now the method is bound to an instance and not the class.

Marius Gedminas said...

Because Python distinguishes unbound methods from global functions.

You can unwrap the function object from the unbound method and get it working this way:

B.foo = A.foo.im_func
b = B()

Jonathan Ellis said...

Marius is correct. I think Mark has the right idea too, just without the standard terminology. :)

Ian Bicking said...

Well, to be technical, it's more the standard metaclass that distinguishes function objects and unbound methods.

When you do ClassObject.attr, the ClassObject looks up 'attr' in its dictionary, and maybe munges that value, and returns it. It doesn't munge many values. It turns functions into UnboundMethods, and with new-style classes it looks for a __get__ method on the object and uses that if present.

The order and function is slightly different for instances; first you ask the instance in that case, then the class, and the class uses a different process (returning a BoundMethod or calling __get__ with slightly different arguments).

That second example (B.foo = A.foo) puts an UnboundMethod in B's __dict__. The standard metaclass doesn't rebind UnboundMethods, it just returns them. And UnboundMethods aren't entirely unbound -- they are bound to a class, but not an instance.

But we're not actually interested in that example in what B.foo returns, we're interested in what b.foo returns. In that case B returns the function foo bound to the class A (an UnboundMethod object), and then tries to bind that method to the instance b. Internally to UnboundMethods there is a check that the object it is bound to ("self") is an instance of the class the method is bound to. And that's why you get the error -- in fact, if an explicit check wasn't built into UnboundMethod, there's no underlying reason that it couldn't work (but it's there because it's typically a bug, and would result in the kind of bug that would drive you absolutely nuts).

Andreas Kostyrka said...


works too :)

Basically this allows "ugly" things like
Runtime assembly of classes:


which makes B acquire all the interesting stuff from A.


Anonymous said...

That is so great!
It's also possible to run python in parallel on SMP: Parallel Python