Adding methods to an object

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Oct 13 09:16:53 EDT 2005


This isn't code of mine, it's probably from the cookbook, maybe with
little changes:

| def addMethod(object, method, name=None):
|     if name is None: name = method.func_name
|     class newclass(object.__class__):
|         pass
|     setattr(newclass, name, method)
|     object.__class__ = newclass

name is the name for the new method, if it's None then the name of
"method" is used.

Bye,
bearophile




More information about the Python-list mailing list