RE: Dynamically altering __init__

2011-11-13 Thread Kääriäinen Anssi
I wrote: I will post a link to a complete example once I have done the AST transformations etc. I hope this will be useful to readers of this list. I didn't find such an example, so maybe the next asker will find it... Finally got time to do this. The example can be found at:

Re: Dynamically altering __init__

2011-10-13 Thread Ian Kelly
2011/10/13 Kääriäinen Anssi anssi.kaariai...@thl.fi: import parser import types st = parser.suite(src) dyn_func = parser.compilest(st) f = types.FunctionType(dyn_func, {}, '__init__') im = types.MethodType(f, None, Foo) Foo() Foo.__init__ = im Foo(1, 2, 3) The result is: TypeError:

RE: Dynamically altering __init__

2011-10-13 Thread Kääriäinen Anssi
Ian Kelly wrote: You can either pull the function code object out of the module code object constants: ... st_code = parser.compilest(st) func_code = st_code.co_consts[0] f = types.FunctionType(func_code, {}, '__init__') ... But you should take care to ensure that the function code object