python3: 'where' keyword

Bengt Richter bokr at oz.net
Sat Jan 8 22:13:26 EST 2005


On 8 Jan 2005 16:13:39 -0800, "Carl Banks" <invalidemail at aerojockey.com> wrote:

>
>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?
Not in any way except that the idea was not to show the best code,
but to show an illustration of possible rules of parsing ;-)
>
>. 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.
I guess that might depend on the local-scope effects of 'where:' --
I think it is too early to jump to conclusions one way or the other.
At this point I just wanted to clarify what the proposal really was,
by asking questions about silly code snippets.

>
>
>> 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.
Just because something contorted is _possible_ is not a good reason for
limiting a general capability IMO. Why not say instead,
"I can only hope no one uses it that way."?
>
>. 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.
I actually like it quite a bit, in spite of the NIH ego thing ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list