[Tutor] Global namespace/dictionary

Keith Winston keithwins at gmail.com
Sat Dec 21 20:22:32 CET 2013


On Sat, Dec 21, 2013 at 12:56 PM, <tutor-request at python.org> wrote:

> py> x = 23
> py> d = globals()
> py> d['x']
> 23
> py> d['x'] = 42
> py> x
> 42
>


Well this is sort of fascinating, but a bit confusing: I was playing with
this, and it seems like the global dictionary includes not just the name
but the entire contents of every dictionary within it... that seems
implausibly redundant, so maybe that's just something about how exploring a
dictionary functions recursively, or something? Maybe it's correct to say
that any dictionaries within a namespace are stored in that namespace,
though that doesn't really sound right.

>>> d = globals()
>>> fubar = {1: 'spam', 'eggs': 2}
>>> d
{'__name__': '__main__', '__builtins__': <module 'builtins' (built-in)>,
'__doc__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'fubar': {1: 'spam', 'eggs': 2}, 'd': {...}, '__package__': None}

And yes, I did change up my string/int order in key/value positions: I'm
fascinated by the flexibility of the data structure. I suspect the answer
to my question lies in exactly this facet.

-- 
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131221/7f5919e5/attachment-0001.html>


More information about the Tutor mailing list