I haven't a clue what you're trying to do, but
I believe it's not possible to install "magic methods"
after the class is defined. Nor can you install
them in the instance. They'll look like they've
been installed, but the interpreter will never
execute them because it does not look there
for them. It looks in special places in the C
language structure for the class definition.
I'm not sure what you mean about not installing methods, but methods
are effectively dynamically created when you access functions via a
class attribute name, so you can tack them on any time before you
access them. If you access them via a class attribute per se, you get
an unbound method, and if you access them via a class instance, you get
a bound (to that instance) method. E.g.,
'This is from a method of B'
Note the distiction between 'foo' and 'method_name'. Once 'foo' has been used to
create the method, only the name of the function is available via the (un)bound method
object reference. They happen to match when you do the def inside the class scope, but
they don't have to, though the repr string might be a little confusing when they don't.
If you use a programming trick to get the function bound as an attribute of a class,
that should work just like doing it explicitly as above.
Note that the unbound method is not just a reference to the function:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method method_name() must be called with A instance as first argument (got B
instance instead)
Though you can sneak around to it:
<function method_name at 0x008FDE70>
And it happens to work as a function 'This is from a method of B'
To get that function we see via A to be an unbound method of B is also possible:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method method_name() must be called with B instance as first argument (got A
instance instead)
Regards,
Bengt Richter