Add methods to a class at runtime?

Stefan Schwarzer sschwarzer at sschwarzer.net
Sun Sep 1 17:32:13 EDT 2002


Hello Robert

Robert Oschler wrote:
> Thanks but I need a little bit more detail.  Suppose I want to do it from
> within code?  For example, I create a function called
> AddBarToFoo(FooObject), that given a object of type Foo (FooObject) adds the
> Bar method to it dynamically, what would be the syntax then?  Would I have
> to put the method declaration code, which you show being typed in to the
> interpreter, in a string and then eval/exec it?  Is there a more convenient
> way than that?

There was a thread on this some time ago:

http://groups.google.de/groups?hl=de&lr=&ie=UTF-8&oe=UTF-8&th=9a6dcf6380b7afe0&seekm=Xns91D662196C970duncanrcpcouk%40127.0.0.1&frame=off

Maybe that is helpful. :)

Stefan




More information about the Python-list mailing list