run time code generation in python

Carlo v. Dango amigo at fake.not
Thu Oct 9 08:21:53 EDT 2003


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. I have tried the compile(str) 
method but I haven't succeeded. I've tried using the __getattribute__ but 
there is a problem in that I do not know if __getattribute__ was called due 
to a method call taking place, or due to someone who just wants a reference 
to a method.. (and for other reasons as well :) I need to runtime generate 
the above method.

hope some one has some good pointers to run time code generation in 
python..

many thanks

Carlo Van Dango





More information about the Python-list mailing list