Why instancemethod when I can add functions to classes outside class body?

Rim rimbalaya at yahoo.com
Fri Jul 25 06:27:38 CEST 2003


It appears to me the simplest way to add a function to a class outside
of the class declaration is as follows:

>>> class a(object):
...    pass
>>> def f(self):
...    print 'hi'
>>> a.f = f
>>> b=a()
>>> b.f()

It works even when the class and the function are in defined in
different modules,
and the call to the method in a third module.

So what problem is the new.instancemethod() trying to solve?

It is ok to point me to a PEP or the like.

- Rim

More information about the Python-list mailing list