run time code generation in python

Robert Brewer fumanchu at amor.org
Thu Oct 9 14:08:36 EDT 2003


"Carlo v. Dango" <amigo at fake.not> wrote in
news:Xns940F929A63B436020206 at 172.16.0.1: 

> 
> Hello there. I have found a need to runtime generate a method and
> instert it into an object instance. The code is a simple forwarding
> mechanism like 
> 
> def foo(self, *args, **kwargs):
>     self.i.foo(*args, **kwargs)
> 
> method.. however, it is only at runtime that I know the name "foo" so
> I cannot gerenate such a method any sooner. 

Duncan Booth wrote:
> You should be aware that if you store a function in an instance at
runtime, 
> the magic binding to 'self' won't happen. You either need to store the

> function in the class, or leave out the self parameter if you want to 
> insert it into an instance.

You can obtain the magic binding to self with the "new" module's
instancemethod() function.

cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list