[Python-ideas] Where-statement (Proposal for function expressions)

Carl Johnson cmjohnson.mailinglist at gmail.com
Wed Jul 15 11:59:39 CEST 2009


Greg Ewing wrote:

Nitpicking:

> * It's not restricted to a single function argument:
>
>  def foo(f, g) where:
>    def f(x):
>      ...
>    def g(y):
>      ...
>

Surely, you meant

foo(f, g) where:
   def f(x):
     ...
   def g(y):
     ...

Ending a statement that ends in a ":" normally with a "where" is too
ambiguous and ought to be prohibited.

I need to think about this proposal some more, but it would help solve
the issue brought up on the list recently that [f(x) for f in fs if
f(x)] is inefficient and [i for i in (f(x) for f in fs) if i] is ugly.

new_list = [fx for f in fs if fx] where:
    fx = f(x)

The "where" on the end of the clause does catch your eye and make you
think "OK, be aware, something is undefined on this line but will be
defined on the next."

OTOH, in

new_list = [i for i in xs
              for j in ys if fx] where:
    fx = f(x)

or

new_list = [i for i in [j for j in ys if fx?] if fx?] where:
    fx = f(x)

Does the "where" get re-used with each loop of the inner list
comprehension or just the outer one?

What about

total = sum(count for item in l) where:
    count = item.size if item.size > 0 else 1

It seems like for that case, we definitely want the "where" to apply
every time the generator expression loops though, but… Is that
feasible?

Hmm.

-- Carl Johnson



More information about the Python-ideas mailing list