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

Sturla Molden sturla at molden.no
Tue Jul 20 21:32:25 CEST 2010

Den 20.07.2010 20:56, skrev Alex Light:
> i would suggest overloading the 'with', 'as' combo
> c = sqrt(a*a + b*b) with:
>     get_a(), get_b() as a, b

That will not work, the parser would think like this:

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

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

I think it tastes too much like functional programming. Specifically:

It forces you to think backwards: it does not evaluate the lines in the 
order they read. The block below is evaluated before the expression 
above. It really means:

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

That I think is very annoying.

Why not just use context managers instead?

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

Now it reads the right way: top down, not bottom up.


More information about the Python-ideas mailing list