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

Jae Kwon jkwon.work at gmail.com
Wed Jul 15 21:36:22 CEST 2009


i like defining things prior to running them. One issue, however, is  
namespace management. i'd like temporary names to be discarded  
automatically.

where:
     def foo(x,y,z):
         return x+y+z
do:
     bar = foo(1,2,3)

print foo(1,2,3) // foo is undefined!
print bar // 6

you could create a local namespace w/ just the 'where' block.

where:
     x = 1
     y = x

print x // x is undefined!


Perhaps we could bind the variables and pass them around.

where my_locals:
     x = 1
     y = 2

print my_locals() // {'x': 1, 'y': 2}
print x // x is undefined!

  - Jae

On Jul 15, 2009, at 12:55 AM, Greg Ewing wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas




More information about the Python-ideas mailing list