[Python-ideas] Statements vs Expressions... why?

Cliff Wells cliff at develix.com
Sun Sep 14 08:36:39 CEST 2008

```On Sun, 2008-09-14 at 07:23 +0100, Arnaud Delobelle wrote:
> On 13 Sep 2008, at 23:17, Cliff Wells wrote:
>
> > On Sat, 2008-09-13 at 19:01 +0100, Arnaud Delobelle wrote:
> >>
> >> So what does:
> >>
> >> a = (if False: 1)
> >>
> >> evaluate to?
> >
> > That's a good question.  This is one of those areas where a definition
> > would need to be created.  My inclination is to say None (much like a
> > function with no return statement).
> >
>
> Assuming the return value of "None", I go back to an example I gave
> earlier:
>
>     factors = for x in range(2, n):
>         if n % x == 0:
>             x
>
> This doesn't work as intended (filtering out the non-factors).  How to
> make it work?  The only way I can think of is to make (if 0: 1) return
> a special "non-value" which loops will then filter out.  But then we
> all know what happens to non-values.
>
> So how would you solve this problem?

By writing it properly ;-)

factors = for x in range ( 2, n ):
if n % x == 0:
yield x

As I mentioned previously, in order to merge the concept of generator
with a for-expression would require bringing in the yield keyword, just
as it does now for generator functions.

The example you gave would evaluate to None (or perhaps an empty list or
generator - that's a detail that would take more consideration before
defining it).

Regards,
Cliff

```