Good hearing from you John.

It's interesting to compare globals( ) with dir( ).  That latter is affected
by
the new names in a for loop but doesn't reflect these changes until
later, whereas globals( ) raises an exception.

The globals dict seems an unusual beast.  Interesting recursivity too.

>>> g = globals()
>>> g['g']['g']['g']['g']['g']['g']['g']['g']['g']
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__',
'__doc__': None, 'g': {...}, '__package__': None}

I call it "flat recursivity" in that we're not looking at successive frames
on some stack.

Hmmm, just stumbled upon (is "recursivity" a word?):
http://recursed.blogspot.com/

Kirby


>>> ================================ RESTART
================================
>>> g = dict(globals())
>>> g
{'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__',
'__doc__': None, '__package__': None}
>>> for t,v in g.items():  print(t,v)

__builtins__ <module 'builtins' (built-in)>
__name__ __main__
__doc__ None
__package__ None
>>> help(globals)
Help on built-in function globals in module builtins:

globals(...)
    globals() -> dictionary

    Return the dictionary containing the current scope's global variables.

>>> d = dir()
>>> for v in d:  print(v)

__builtins__
__doc__
__name__
__package__
g
t
v
>>> ================================ RESTART
================================
>>> d = dir()
>>> for v in d:  print(v)

__builtins__
__doc__
__name__
__package__
>>> g = globals()
>>> for v in g:  print(v)

d
g
__builtins__
__package__
v
__name__
__doc__
>>> ================================ RESTART
================================
>>> g = globals()
>>> for v in g:  print(v)

__builtins__
Traceback (most recent call last):
  File "<pyshell#178>", line 1, in <module>
    for v in g:  print(v)
RuntimeError: dictionary changed size during iteration
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'g', 'v']
>>> ================================ RESTART
================================
>>> d = dir()
>>> for v in d:  print(v)

__builtins__
__doc__
__name__
__package__
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'd', 'v']
>>>
_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to