how to dynamically create class methods ?

j vickroy jim.vickroy at noaa.gov
Tue Mar 25 14:13:47 EDT 2008


Hello,

Here is some pseudo-code that hopefully illustrates what I want to do:

records = list(...)
for record in records:
    new_fcn = define_a function_for(record)
    instance = my_new_class_instance()
    setattr(instance, 'myfcn', new_fcn)
    instance.execute() # instance.execute() calls instance.myfcn(*args)


I have looked at some of the functions in the *new* module and 
new.code(...), new.function(...), and new.instancemethod(...) appear to 
do what I want, but I do not know how to use new.code() and 
new.function() -- specifically what its *global* parameter should be.

I was not able to find helpful information using Google.

Thanks for any suggestions on how to approach this.

-- jv



More information about the Python-list mailing list