[Python-ideas] Where-statement (Proposal for function expressions)
Bruce Frederiksen
dangyogi at gmail.com
Fri Jul 17 04:19:59 CEST 2009
Mike Meyer wrote:
> I disagree with both these conclusions. Clearly, they are paired, as
> they serve a similar purpose, but I don't see that they need to be
> disallowed at all. That they cause problems with your first attempt at
> describing an implementation certainly isn't reason to disallow them
> all by itself.
>
Let me toss out an alternate implementation.
statement where:
body
creates a new temporary local context and evaluates the body in it. Any
variables assigned to within the body are set in this temporary context
(as you'd expect). Variables in the outer context are still visible to
the body.
Now the difference:
Then the statement is run within this temporary local context, but with
a change in that now all variables assigned to bypass the temporary
context (perhaps unbinding the variable there, if it's set, so that it
no longer hides the outer value?) and are set in the outer context. But
variables within the temporary context are still visible to the
statement. This is a new use of contexts for Python.
Once the statement is finished executing, the temporary context is
discarded.
Thus:
i = 2
x[27] = 2
x[i] = 4 where:
i = 27
print(x[27]) # 4
print(i) # 2
has access to the temporary i (set to 27).
While:
x = 1
x = i where:
i = 27
print(x) # 27
assigns 27 to x in the outer context.
Thus, the where clause applies to the whole statement, without limiting
the statement's ability to bind variables in the outer context.
-Bruce
More information about the Python-ideas
mailing list