eval, exec and execfile dilemma

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 31 01:31:21 CEST 2011

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

Have you considered not using exec at all, and using a good old-fashioned
factory function and closures?

def factory(x):
    def inner(param):
        return param + x
    return inner

plusone = factory(1)
plustwo = factory(2)

I'm betting that this will be much faster than exec, and much more readable.


More information about the Python-list mailing list