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

Rim rimbalaya at yahoo.com
Fri Jul 25 00:27:38 EDT 2003


Hi,

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()
hi

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.

Thanks,
- Rim




More information about the Python-list mailing list