Dynamically adding methods to objects?

Holger Türk htx1 at gmx.de
Sun Jun 6 04:52:52 EDT 2004



damian birchler wrote:
> I know how to do it manually, I just define a function and assigne it
> to f -
> 
> def bar():
>   pass
> 
> f.bar = bar -,
> but what if this should be done at runtime. How can I get the string
> passed to f.add_function as name to be the name of a new function in
> the function's definiton?

f.bar = bar   is the same as   setattr (f, "bar", bar)

Remember that bar does not become a method of f, but remains
only a function which is stored in f. Is not subject of the
binding of the "self" parameter.

Greetings,

Holger




More information about the Python-list mailing list