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

Bengt Richter bokr at oz.net
Mon Nov 1 15:34:30 EST 2004


On Mon, 01 Nov 2004 12:59:01 GMT, exarkun at divmod.com wrote:

>On Mon, 01 Nov 2004 07:20:51 GMT, bokr at oz.net (Bengt Richter) wrote:
>>On Mon, 01 Nov 2004 03:11:42 GMT, exarkun at divmod.com wrote:
>> 
>> >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.
>> >
>> I had a sneaky suspicion that it _could_ be so, hoping not. But your example doesn't play fair
>> because you don't give x an initial condition inside the scope of the expression. E.g.,
>> 
>>  >>> [x for x in [0] for x in iter(lambda:x+1, 4)]
>>  [1, 2, 3]
>>  >>> list(x for x in [0] for x in iter(lambda:x+1, 4))
>>    File "<stdin>", line 1
>>      list(x for x in [0] for x in iter(lambda:x+1, 4))
>>               ^
>>  SyntaxError: invalid syntax
>> 
>
>  I'm not sure what "playing fair" means here :)  I gave an example of a list comprehension that behaves differently when trivially rewritten as a generator comprehension, without relying on the one behavioral difference you explicitly omitted.  I do appear to have been wrong about this change applying to the number_in_base function, though.
I meant you didn't provide an initial value for x inside the expression
like my number_in_base function, or [x for x in [0] ... above.
You did fair-and-square provide an example that won't translate to the
new gx context though. But it appears this is going to be a documentation
problem rather than a technical one, somewhat similar to explaining what
lambda:x+1 will use inside of a function. I.e., it depends on what else is
in the function and its environment.

BTW, did you try the above?
>From a post (with broken subject line ;-) of Steven Bethard's, it seems to work.
I'll re-quote it here to give it context:
----
>Bengt Richter <bokr <at> oz.net> writes:
>> 
>> Well, I don't have 2.4 yet, but what does it do?

>>>> [x for x in [0] for x in iter(lambda:x+1, 4)]
>[1, 2, 3]
>>>> list(x for x in [0] for x in iter(lambda:x+1, 4))
>[1, 2, 3]

>Looks like it does just what you'd hope it would do.  =)

>Steve
----


Regards,
Bengt Richter



More information about the Python-list mailing list