[Python-ideas] Statements vs Expressions... why?
Greg Ewing
greg.ewing at canterbury.ac.nz
Mon Sep 15 02:37:55 CEST 2008
Cliff Wells wrote:
> factors = for x in range ( 2, n ):
> if n % x == 0:
> yield x
Is this construct meant to be a list comprehension or
a generator expression? If it's a GC, you just get
factors bound to an iterator, not a list of results
from that iterator. If it's an LC, then what do you
do if you want a dict or tuple comprehension instead?
Also, 'yield' already has a meaning that's different
from what you seem to want here. Consider:
def foo(n):
for x in range ( 2, n ):
if n % x == 0:
yield x
Here the whole function is a generator, and the yield
causes an item to be returned to the generator's caller.
Now, if there is no difference between a statement and
an expression, the following should do exactly the
same thing:
def foo(n):
factor = for x in range ( 2, n ):
if n % x == 0:
yield x
--
Greg
More information about the Python-ideas
mailing list