globals()
returns a dict
- therefore, to iterate through the 21> key / value pairs, you call it a items()
method:
dog = 'cat'
>>> for name, value in globals().items():
... print(name, value)
...
('__builtins__', <module '__builtin__' (built-in)>)
('__name__', '__main__')
('dog', 'cat')
('__doc__', None)
('__package__', None)
>>>
This is explained in the documents for Data Structures available here!
Python 3 version:
for name, value in globals().copy().items():
print(name, value)
if we do not copy the output functions of the globals, then there will be a RuntimeError.
, , deepcopy() copy()
for name, value in globals().deepcopy().items():
print(name, value)