By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

globals() using For Loop against Generator

P: n/a
if __name__ == '__main__':

print "Globals (For Loop):"
try:
for i in globals():
print "\t%s" % i
except RuntimeError:
print "Only some globals() printed\n"
else:
print "All globals() printed\n"

print "Globals (Generator):"
try:
print "\n".join("\t%s" % i for i in globals())
except RuntimeError:
print "Only some globals() printed\n"
else:
print "All globals() printed\n"
>>>
Globals (For Loop):
__builtins__
Only some globals() printed

Globals (Generator):
__builtins__
__name__
__file__
i
__doc__
All globals() printed
Why is it with a generator I get everything out but with a for loop I
don't? I know that globals is not read-only but I would of expected
the same behaviour from both...

Any thoughts?
Mar 18 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Mel
co*********@gmail.com wrote:
if __name__ == '__main__':

print "Globals (For Loop):"
try:
for i in globals():
print "\t%s" % i
except RuntimeError:
print "Only some globals() printed\n"
else:
print "All globals() printed\n"

print "Globals (Generator):"
try:
print "\n".join("\t%s" % i for i in globals())
except RuntimeError:
print "Only some globals() printed\n"
else:
print "All globals() printed\n"
>>>Globals (For Loop):
__builtins__
Only some globals() printed

Globals (Generator):
__builtins__
__name__
__file__
i
__doc__
All globals() printed

Why is it with a generator I get everything out but with a for loop I
don't? I know that globals is not read-only but I would of expected
the same behaviour from both...
Run the for loop in the interpreter, without catching exceptions. You get

__builtins__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
Then `print globals()` shows that i has been added to the global
namespace. If you run the for loop a second time, after i exists, the
loop runs fine.

Apparently, generator comprehensions have been optimized so that they
don't expose their working variables. The generator comprehension
won't add i to the global namespace, so all is well.

Mel.
Mar 18 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.