dumping generator

Ulrich Eckhardt eckhardt at satorlaser.com
Mon Aug 9 06:44:38 EDT 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




More information about the Python-list mailing list