embedding python in python
Maurice LING
mauriceling at acm.org
Thu Sep 30 20:49:43 EDT 2004
>
> >>> myglobals = {}
> >>> mylocals = {}
> >>> exec "b = 1 + 2" in myglobals, mylocals
> >>> mylocals
> {'b': 3}
> >>> exec "c = b * 2" in myglobals, mylocals
> >>> mylocals
> {'c': 6, 'b': 3}
> >>> >>>
>
> The exec statement can take an optional pair of dictionaries that it
> uses as the global and local namespaces to execute the given code in.
> This is *much* preferable to a bare exec for several reasons, one of
> them being exactly your issue, and another being safety -- since you
> must explicitly include any names that you want exec to use, it makes
> exec rather less likely to unintentionally stomp all over your namespace
> (or for malicious code to do certain types of harm).
>
> Jeff Shannon
> Technician/Programmer
> Credit International
>
Please pardon me for my dumbness but what exactly goes on in
exec "b = 1 + 2" in myglobals, mylocals
?
Thanks
Maurice
More information about the Python-list
mailing list