"number-in-base" ``oneliner''

exarkun at divmod.com exarkun at divmod.com
Mon Nov 1 04:11:42 CET 2004

On Sun, 31 Oct 2004 19:00:07 GMT, bokr at oz.net (Bengt Richter) wrote:
> [snip]
> BTW, will anything that works in a list comprehension work in a generator expression
> (assuming one does not depend on the generator expression having leftover outside
> side effect bindings like the LC version)?

  Nope.  For example, I don't think the code in this thread will work if converted to a generator expression.  A simplified example:

    >>> x = 0 
    >>> list(x for x in iter(lambda: x + 1, 4))
    ( runs forever, so C-c )
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 1, in <generator expression>
    >>> [x for x in iter(lambda: x + 1, 4)]
    [1, 2, 3]

  `x' in the lambda in the list comprehension resolves to a different name than `x' in the generator comprehension.


More information about the Python-list mailing list