python3: 'where' keyword

Carl Banks invalidemail at aerojockey.com
Sat Jan 8 19:13:39 EST 2005


Bengt Richter wrote:
> And, is the whole thing after the '=' an expression? E.g.,
>
>   x = ( foo(x) where:
>          x = math.pi/4.0
>       ) where:
>          def foo(x): print 'just for illustration', x

How would that be any improvement over this?

. x = foo(x) where:
.     x = math.pi/4.0
.     def foo(x): print 'just for illustration', x

Can anyone think of a use case for embedding "where" inside an
expression as opposed to making it part of a simple statement?  And, if
so, is the benefit of it worth the massive hit in readability.


> or is this legal?
>
>   for y in ([foo(x) for x in bar] where:
>                  bar = xrange(5)
>             ): baz(y) where:
>                 def baz(arg): return arg*2

Here, I can only hope not.  One reason I proposed a where...do syntax
is so that, if you wanted to localize a variable to a for loop or some
other compound statement, you could do it with a minimum of fuss.

. where:
.     bar = xrange(5)
.     def baz(arg): return arg*2
. do:
.     for y in [foo(x) for x in bar]:
.         baz(y)


> Not trying to sabotage the idea, really, just looking for
clarification ;-)

That's ok.  For it fly, it's got to be able to withstand the flak.
-- 
CARL BANKS




More information about the Python-list mailing list