Re: eval, exec and execfile dilemma

2011-07-31 Thread Laszlo Nagy
100 functions/second installed to global namespace doesn't sound well. What on earth are you doing needing to use exec to create hundreds of functions?? :-) Have you considered not using exec at all, and using a good old-fashioned factory function and closures? def factory(x): def i

Re: eval, exec and execfile dilemma

2011-07-30 Thread Steven D'Aprano
Laszlo Nagy wrote: > 100 functions/second > installed to global namespace doesn't sound well. What on earth are you doing needing to use exec to create hundreds of functions?? Have you considered not using exec at all, and using a good old-fashioned factory function and closures? def facto

Re: eval, exec and execfile dilemma

2011-07-30 Thread Laszlo Nagy
UnboundLocalError: local variable 'bar' referenced before assignment This works, though (at least it does on 2.7): --> exec "def foo():\n\tglobal bar\n\tbar+=1\n\treturn 1\n" --> bar = 9 --> foo() 1 --> bar 10 Laszlo, why do you think you can't use exec? I'm sorry, first I was not aware of

Re: eval, exec and execfile dilemma

2011-07-30 Thread Ethan Furman
Chris Angelico wrote: On Sat, Jul 30, 2011 at 8:48 PM, Laszlo Nagy wrote: the exec statement can be used to execute a def statement. However, I see no way to change the globals, so I cannot use the exec statement. A quick test in Python 2.4.5: exec "def foo():\n\tbar+=1\n\treturn 1\n" bar=2

Re: eval, exec and execfile dilemma

2011-07-30 Thread Chris Angelico
On Sat, Jul 30, 2011 at 8:48 PM, Laszlo Nagy wrote: > the exec statement can be used to execute a def statement. However, I see no > way to change the globals, so I cannot use the exec statement. You can't use: exec "blah blah" in globals() ? I've not used exec, nor its Python 3 equivalent exe

eval, exec and execfile dilemma

2011-07-30 Thread Laszlo Nagy
Hi, I have a program that generates source code for a function. I need to compile that function so that it can be executed (relatively) fast later. In most cases, I'll be generating about 10 functions per second, and evaluate them frequently. But sometimes I'll have to generate 100 functio