[Python-ideas] 'where' statement in Python?

MRAB python at mrabarnett.plus.com
Tue Jul 20 21:15:04 CEST 2010


Alex Light wrote:
> i would suggest overloading the 'with', 'as' combo
> 
> c = sqrt(a*a + b*b) with:
>     get_a(), get_b() as a, b
> 
> or
> 
> 
> c = sqrt(a*a + b*b) with:
>     get_a() as a
>     get_b() as b
> 
Why use 'as'? Why not:

     c = sqrt(a*a + b*b) with:
         a = get_a()
         b = get_b()

which is like:

     def _():
         a = get_a()
         b = get_b()

         return sqrt(a*a + b*b)

     c = _()
     del _

> 
> it reads well and plus it follows since this statement acts similarly to 
> a regular with and as statement with the behavior of the context manager 
> being (in psudocode): 
> set up: store original value of variable if any and set variable to new 
> value.
> tear down: set value back to original or delete from local namespace if 
> it never had one
> 
> additionally we do not need to introduce any new keywords
> 
> any way that this is implemented though it would be quite useful
> 
> On Tue, Jul 20, 2010 at 2:35 PM, George Sakkis <george.sakkis at gmail.com 
> <mailto:george.sakkis at gmail.com>> wrote:
> 
>     On Tue, Jul 20, 2010 at 8:29 PM, Daniel Stutzbach
>     <daniel at stutzbachenterprises.com
>     <mailto:daniel at stutzbachenterprises.com>> wrote:
> 
>      > On Tue, Jul 20, 2010 at 1:16 PM, Guido van Rossum
>     <guido at python.org <mailto:guido at python.org>> wrote:
>      >>
>      >> Given the large number of Python users familiar with SQL compared to
>      >> those familiar with Haskell, I think we'd do wise to pick a
>     different
>      >> keyword instead of 'where'. I can't think of one right now though.
>      >
>      > Taking a cue from mathematics, how about "given"?
>      >
>      > c = sqrt(a*a + b*b) given:
>      >     a = retrieve_a()
>      >     b = retrieve_b()
> 
>     Or if we'd rather overload an existing keyword than add a new one,
>     "with" reads well too.
> 



More information about the Python-ideas mailing list