dumping generator

Discussion in 'Python' started by targetsmart, Aug 9, 2010.

  1. targetsmart

    targetsmart Guest

    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]
    targetsmart, Aug 9, 2010
    #1
    1. Advertising

  2. targetsmart

    Peter Otten Guest

    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
    Peter Otten, Aug 9, 2010
    #2
    1. Advertising

  3. 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
    Ulrich Eckhardt, Aug 9, 2010
    #3
  4. 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.
    Rob Williscroft, Aug 9, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    4,770
    Peter
    Apr 19, 2006
  2. TheDustbustr
    Replies:
    1
    Views:
    430
    Sami Hangaslammi
    Jul 25, 2003
  3. Replies:
    9
    Views:
    524
  4. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    1,666
    Max M
    Sep 12, 2006
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    444
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page