Dangerous behavior of list(generator)

Discussion in 'Python' started by Tom Machinski, Dec 13, 2009.

  1. In most cases, `list(generator)` works as expected. Thus,
    `list(<generator expression>)` is generally equivalent to `[<generator
    expression>]`.

    Here's a minimal case where this equivalence breaks, causing a serious
    and hard-to-detect bug in a program:

    >>> def sit(): raise StopIteration()

    ...
    >>> [f() for f in (lambda:1, sit, lambda:2)]

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 1, in sit
    StopIteration
    >>> list(f() for f in (lambda:1, sit, lambda:2))

    [1]

    I was bitten hard by this inconsistency when sit() was returning the
    idiom `(foo for foo in bar if foo.is_baz()).next()`. The nonexistence
    of a foo with is_baz() True in that query raises an exception as
    designed, which expresses itself when I use the list comprehension
    version of the code above; the generator version muffles the error and
    silently introduces a subtle, confusing bug: `lambda:2` is never
    reached, and a truncated list of 1 element (instead of 3) is
    "successfully" generated..

    Just wondered what you guys think,

    -- Tom
     
    Tom Machinski, Dec 13, 2009
    #1
    1. Advertisements

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:
    6,074
    Peter
    Apr 19, 2006
  2. TheDustbustr
    Replies:
    1
    Views:
    648
    Sami Hangaslammi
    Jul 25, 2003
  3. Replies:
    9
    Views:
    882
  4. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    2,121
    Max M
    Sep 12, 2006
  5. Gabriel Genellina

    Re: Dangerous behavior of list(generator)

    Gabriel Genellina, Dec 13, 2009, in forum: Python
    Replies:
    14
    Views:
    506
    Martin v. Loewis
    Jan 2, 2010
  6. Replies:
    10
    Views:
    569
    Peter Otten
    Dec 14, 2009
  7. Replies:
    3
    Views:
    344
    Steven D'Aprano
    Dec 15, 2009
  8. Replies:
    4
    Views:
    343
    Martin v. Loewis
    Jan 2, 2010
Loading...