how to make a code object a function

Diez B. Roggisch deets_noospaam at web.de
Sun Jan 4 17:34:41 CET 2004


> You could provide a separate namespace and then extract only the
> callables:
> 
>>>> d = {}
>>>> exec "factor=2\ndef alpha(s, t): print factor*t" in d
>>>> d.keys()
> ['__builtins__', 'alpha', 'factor']
>>>> funcs = dict([(n,f) for n, f in d.iteritems() if callable(f)])
>>>> funcs
> {'alpha': <function alpha at 0x4029025c>}
>>>> funcs["alpha"](None, 2)
> 4

And again I learned something completely new - cool. Thanks,

Diez



More information about the Python-list mailing list