473,473 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

globals() using For Loop against Generator

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
1 1836
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
3
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating...
3
by: Brian L. | last post by:
After following the discussion on Generator expressions on python-dev for the last few weeks, and finally getting used to list comprehensions as opposed to map/filter, it occurred to me that the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.