Dynamically adding methods to objects?
John Roth
newsgroups at jhrothjr.com
Sun Jun 6 06:59:24 EDT 2004
"Holger Türk" <htx1 at gmx.de> wrote in message
news:c9um21$hca$03$1 at news.t-online.com...
>
>
> 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.
Ah, but that's what I want to do - have a ***method***
where the self parameter works the way I expect it to.
John Roth
>
> Greetings,
>
> Holger
>
More information about the Python-list
mailing list