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

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Jul 15 09:55:15 CEST 2009


Carl Johnson wrote:

> My suggestion for future would be language extenders is that they try
> to solve a broader problem than just the "how do I make callbacks more
> convenient" problem.

One such extension might be a "where" block. Applied
to the current problem:

   foo(f) where:
     def f(x):
       ...

Benefits include:

* It's pretty much self-explanatory, as it builds on
   the existing syntax for defining functions, and
   "where" is used in a similar way in mathematics and
   some existing programming languages.

* You get to give the function a meaningful name if
   you want.

* It's not restricted to a single function argument:

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

* It needn't be restricted to functions:

   foo(x, y) where:
     x = a * 42
     y = b / 17

Drawbacks include:

* Execution happens out of order, although that's an
   inherent feature of the original proposal as well.
   There are precedents in the language already, such
   as list comprehensions and conditional expressions.

* Allowing arbitrary statements in the block could possibly
   be considered detrimental to code readability. To mitigate
   that, the block contents could perhaps be restricted to
   binding constructs only.

-- 
Greg



More information about the Python-ideas mailing list