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

Paul Moore p.f.moore at gmail.com
Thu Jul 16 20:51:43 CEST 2009


2009/7/16 Chris Perkins <chrisperkins99 at gmail.com>:
>> You've also disallowed
>>
>> x[i] = 12 where:
>>    i = some_complicated_expression()
>>
>> Was that deliberate? If so, could you explain why now, so that it's on
>> record for the legions of people who will ask after it's implemented?
>> :-)
>
> Well, my original answer was "no, I just didnt' think of that'.  But
> now that you mention it, I think in the case of an "augassign"
> production, it makes sense to have the left hand side evaluated in the
> current scope (not in the 'where block" scope).  So in your examle,
> you would either get a NameError for i, the code would do somthing
> that you didn't expect.

And yet the user's intent is obviously to use the i defined inside the
where (that's how functional languages work, so it's hardly an
unrealistic intent).

I'd argue that having this example use a previously defined value of i, when

x = i where:
    i = some_complicated_expression()

uses the i defined in the where clause, is a disaster waiting to happen.

On another point that came up -

i = 5
print i # 5
x[i] = 12 where:
    global i
    i = 3
print i # 3

seems to me to be so unexpected and non-obvious that it makes the
whole construct suspect. Yes, I know that the semantics, as given in
terms of a rewrite, define what happens, but it seems to me that it
goes counter to intuition.

Thinking about it, the issue is that you're making where the *only*
example in Python (that i can think of) of a non-defining construct
which creates a new scope. Currently, scopes are created by the class
and def statements. Both of these define objects (classes and
functions, respectively). The where clause as you've stated it defines
a scope, but no object. That doesn't make it ill-defined, but it does
make it (very, in my view) non-intuitive.

Paul.



More information about the Python-ideas mailing list