import from a string

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Nov 4 01:02:34 EST 2009


En Wed, 04 Nov 2009 02:45:23 -0300, iu2 <israelu at elbit.co.il> escribió:
> On Nov 4, 3:10 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:

>> txt = """
>> def foo(x):
>>    print 'x=', x
>>
>> def bar(x):
>>    return x + x
>> """
>>
>> py> namespace = {}
>> py> exec txt in namespace
>> py> namespace.keys()
>> ['__builtins__', 'foo', 'bar']
>> py> namespace['foo']('hello')
>> x= hello

> What happens if both global and local dictionaries are supplied: where
> are the newly created entities created? In the local dict?

The amazing thing about Python is how easy is to experiment in the  
interpreter.
Just see it by yourself!

-- 
Gabriel Genellina




More information about the Python-list mailing list