python3: 'where' keyword
ncoghlan at iinet.net.au
Sat Jan 8 07:08:43 CET 2005
Andrey Tatarinov wrote:
> It would be great to be able to reverse usage/definition parts in
> haskell-way with "where" keyword. Since Python 3 would miss lambda, that
> would be extremly useful for creating readable sources.
> Usage could be something like:
> >>> res = [ f(i) for i in objects ] where:
> >>> def f(x):
> >>> #do something
Hmm, this is actually a really interesting idea. Avoiding accidental namespace
conflicts is certainly one of the advantages of using lambdas.
This idea has the virtue of being able to do the same thing, but have full
access to Python's function syntax and assignment statements in the 'expression
In fact, any subexpressions in a complicated expression can be extracted and
named for clarity without fear of screwing up the containing namespace, which
would be an enormous boon for software maintainers.
It also allows the necessary but uninteresting setup for an expression to be
moved "out of the way", bringing the expression that does the real work to
From the interpreter's point of view, the meaning would probably be something like:
namespace = locals()
exec where_suite in globals(), namespace
exec statement in globals(), namespace
res = namespace["res"]
Making the 'where' clause part of the grammar for the assignment statement
should be enough to make the above example parseable, too.
The clause might actually make sense for all of the simple statement forms in
the grammar which contain an expression:
augmented assignment statement
The clause really isn't appropriate for break, continue, import or global
statements, as they don't contain any expressions :)
For compound statements, a where clause probably isn't appropriate, as it would
be rather unclear what the where clause applied to.
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
More information about the Python-list