exec("dir()",d)

Christian Heimes lists at cheimes.de
Sun Aug 9 15:01:48 CEST 2009


Emanuele D'Arrigo schrieb:
> Greetings everybody,
> 
> I don't quite understand why if I do this:
> 
>>>> d = {}
>>>> exec("dir()", d)
> 
> 1) d is no longer empty
> 2) the content of d now looks like __builtins__.__dict__ but isn't
> quite it d == __builtins__.__dict__ returns false.
> 
> Can anybody shed some light?

RTFM helps ;) http://docs.python.org/reference/simple_stmts.html#exec

As a side effect, an implementation may insert additional keys into the 
dictionaries given besides those corresponding to variable names set by 
the executed code. For example, the current implementation may add a 
reference to the dictionary of the built-in module __builtin__ under the 
key __builtins__ (!).

Christian




More information about the Python-list mailing list