python3: 'where' keyword
ncoghlan at iinet.net.au
Sun Jan 9 07:58:24 CET 2005
Paul Rubin wrote:
> Nick Coghlan <ncoghlan at iinet.net.au> writes:
>>I think having to keep the names unique within the statement you are
>>currently writing is a reasonable request :)
> Um, you could say the same thing about the function, the module, etc. ;)
And, indeed, that is what Python currently says. When writing code, the relevant
namespaces are the builtins, the module, any containing functions and the
current function. Inadvertent conflicts with any of those can have surprising
The idea of 'where' is to push that down one level, and allow a namespace to be
associated with a single statement.
Trying to push it a level further (down to expressions) would, IMO, be a lot of
effort for something which would hurt readability a lot.
x = sqrt(a) + sqrt(b) where:
a = 2.0
b = 3.0
This brings the operation we care about (add the sqrt's of 2 and 3) right up
front. A folding code editor could actually hide the details quite easily. We
can look inside the statement if we want to know what x & y actually are.
x = (sqrt(a) where:
a = 2.) \
+ sqrt (a) where:
a = 3.
We haven't gotten rid of anything here - all the stuff we're interested in
clearing out of the way is still embedded in the middle of our statement.
Also not insignificantly, we're trying to put a suite inside an expression,
which will be rejected for all the reasons that have kept lambda restricted to a
single expression despite numerous complaints over time.
Now, nothing in the idea of a statement local namespace actually *rules out* the
prospect of an expression local namespace, so it could be added at a later date.
However, doing so would require some actual use cases, and an
expression-friendly syntax. Perhaps something that involves providing the
namespace directly, like:
x = (sqrt(a) where (a=2.0)) + (sqrt(b) where (a=3.0))
It seems to make more sense to try for statement local namespaces *first*, and
then see if expression local namespaces are worth it.
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
More information about the Python-list