Dennis Lee Bieber
3/14/2008 6:36:00 AM
On Thu, 13 Mar 2008 21:35:42 +0100, Stef Mientki
<stef.mientki@gmail.com> declaimed the following in comp.lang.python:
> The result of globals and locals in the file is eaxctly the same and
> none of them mentions 'NewVar' and 'beer'
> The result of tree_globs and tree_locals is exactly the same and both
> doesn't contain 'NewVar' and 'beer'
> Maybe it's very simple after reading the manual,
> but apperently I'm missing the essential clue.
> What am I missing ?
>
Use of globals() and locals() vs globals and locals?
-=-=-=-=- execsub.py
beer = 'testje'
print dir()
print "dir(globals)", dir (globals)
print "dir(locals) ", dir (locals)
print "globals() ", globals()
print "locals() ", locals()
-=-=-=-=-
>>> sub_globs = {"newvar" : 42}
>>> sub_locals = {}
>>> execfile("execsub.py", sub_globs, sub_locals)
['beer']
dir(globals) ['__call__', '__class__', '__cmp__', '__delattr__',
'__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__str__']
dir(locals) ['__call__', '__class__', '__cmp__', '__delattr__',
'__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__str__']
globals() {'__builtins__': {'IndexError': <class exceptions.IndexError
<snip>
>>> print sub_locals
{'beer': 'testje'}
>>>
I don't have an explanation for why "newvar" doesn't make it into
the sub file, but as can be seen, the sub local update did make it back
out...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/