Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dumping generator

Reply
Thread Tools

dumping generator

 
 
targetsmart
Guest
Posts: n/a
 
      08-09-2010
Right now if I want to dump the contents of a generator object I use ,
a snip from a bigger block of code..

try:
while gen: print gen.next()
except StopIteration:
print "Done"
else:
raise

is there a much simpler way ?

like for printing list we do
list = range(10)
print list
would print
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-09-2010
targetsmart wrote:

> Right now if I want to dump the contents of a generator object I use ,
> a snip from a bigger block of code..
>
> try:
> while gen: print gen.next()
> except StopIteration:
> print "Done"
> else:
> raise
>
> is there a much simpler way ?


Indeed there is:

for item in gen:
print item
print "Done"

Peter

 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-09-2010
targetsmart wrote:
> Right now if I want to dump the contents of a generator object I use ,
> a snip from a bigger block of code..
>
> try:
> while gen: print gen.next()
> except StopIteration:
> print "Done"
> else:
> raise
>
> is there a much simpler way ?


Why not something like this:

for i in gen:
print i

> like for printing list we do
> list = range(10)
> print list
> would print
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


You could coerce the thing into a list:

gen = xrange(10)
print list(gen)



Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      08-09-2010
targetsmart wrote in news:cd83533b-f51e-4955-96c5-f8a10185bef1
@i18g2000pro.googlegroups.com in gmane.comp.python.general:

> Right now if I want to dump the contents of a generator object I use ,
> a snip from a bigger block of code..
>
> try:
> while gen: print gen.next()
> except StopIteration:
> print "Done"
> else:
> raise
>
> is there a much simpler way ?


print list( gen )

>
> like for printing list we do
> list = range(10)
> print list
> would print
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Rob.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generator functions subclass generator? Terry Reedy Python 0 06-18-2009 09:51 PM
Problems with email.Generator.Generator Chris Withers Python 20 09-12-2006 11:13 AM
UART with fractional baudrate generator ? Or fractional baudrate generator alone Martin Maurer VHDL 3 04-19-2006 01:26 PM
subtle side effect of generator/generator expression bonono@gmail.com Python 9 10-16-2005 06:42 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments