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

exarkun at divmod.com exarkun at divmod.com
Sun Oct 31 22:11:42 EST 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>
    KeyboardInterrupt
    >>> [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.

  Jp



More information about the Python-list mailing list