Given the following two classes,
class A: def foo(self): print self.__class__.__name__ class B: pass
why does this work,
def bar(self): print self.__class__.__name__ B.foo = bar b = B() b.foo()
while this does not?
B.foo = A.foo b = B() b.foo()
Given the following two classes,
class A: def foo(self): print self.__class__.__name__ class B: pass
why does this work,
def bar(self): print self.__class__.__name__ B.foo = bar b = B() b.foo()
while this does not?
B.foo = A.foo b = B() b.foo()
Comments
A.foo()
calling the instance method on the class not an instance.
what of course does work is:
a=A()
b=B()
b.foo = a.foo
b.foo()
because now the method is bound to an instance and not the class.
You can unwrap the function object from the unbound method and get it working this way:
B.foo = A.foo.im_func
b = B()
b.foo()
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).
B().bar()
works too :)
Basically this allows "ugly" things like
Runtime assembly of classes:
B.__dict__.update(A.__dict__)
which makes B acquire all the interesting stuff from A.
Andreas
It's also possible to run python in parallel on SMP: Parallel Python