Re: exec(dir(),d)

2009-08-10 Thread Emanuele D'Arrigo
Thank you both for the explanation.

As a matter of fact RTFM doesn't -always- help. Sometimes I'm just
thick and I can read the manual 10 times and still not understand, as
it happened on this particular matter. Your contribution focused my
attention on the right bit of the manual which I somehow didn't manage
to ingest before. Now I understand.

Thank you.

Manu
-- 
http://mail.python.org/mailman/listinfo/python-list


exec(dir(),d)

2009-08-09 Thread Emanuele D'Arrigo
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?

Manu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec(dir(),d)

2009-08-09 Thread Mel
Emanuele D'Arrigo wrote:

 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?

You should check up on what exec does.  In this case it runs a string 
containing code using dictionary `d` as its global namespace, and so `d` has 
to contain the usual global namespace symbols -- otherwise `dict` can't 
work.  `__builtins__.__dict__ is one of the elements in the global 
namespace, not the namespace itself.  Another example:


Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type help, copyright, credits or license for more information.
 a=7
 d={'b':a}
 exec (print b, d)
7
 d
{'__builtins__': {'bytearray': type 'bytearray', 'IndexError': type 
'exceptions.IndexError', 'all': built-in function all, 'help': Type 
help() for interactive help, or help(object) for help about object., 'vars': 
built-in function vars, 'SyntaxError': type 'exceptions.SyntaxError', 
'unicode': type 'unicode', 'UnicodeDecodeError': type 
'exceptions.UnicodeDecodeError', 'isinstance': built-in function 
isinstance, 'copyright': Copyright (c) 2001-2009 Python Software 
Foundation.
[ ... ]
'AssertionError': type 'exceptions.AssertionError', 'classmethod': type 
'classmethod', 'UnboundLocalError': type 'exceptions.UnboundLocalError', 
'NotImplementedError': type 'exceptions.NotImplementedError', 
'AttributeError': type 'exceptions.AttributeError', 'OverflowError': type 
'exceptions.OverflowError'}, 'b': 7}
 

To get rid of the name error, you'd need
d={}
d['d'] = d
exec (dir (d), d)


Mel.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec(dir(),d)

2009-08-09 Thread Christian Heimes

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

--
http://mail.python.org/mailman/listinfo/python-list