Statement local namespaces summary (was Re: python3: 'where' keyword)
ncoghlan at iinet.net.au
Sun Jan 9 07:43:44 EST 2005
Andrey Tatarinov wrote:
> So it seems that people loved the idea of 'where' keyword, may be it's
> time to think about PEP draft? I appreciate any help (cause my english
> is not that good =)).
There's certainly a PEP in the idea. Here's a summary of what we have so far in
this thread in a PEPish format. Any reference implementation should definitely
wait for the compiler upgrade to hit the main development branch in CVS though
(or be based on ast-branch if someone gets inspired in the interim).
The proposal is to add the capacity for statement local namespaces to Python.
This allows a statement to be placed at the current scope, while the statement's
'setup code' is indented after the statement::
The main benefit is the avoidance of namespace pollution in the outer scope.
Sections of the statement can be extracted and named in the statement's local
namespace without any inadvertent side effects due to name conflicts.
The second benefit is that it can improve readability by allow easy grouping
of the setup code for a given expression (see the examples section)
Thirdly, it provides full-fledged effectively anonymous functions (just
define them in the statement's local namespace).
The idea is inspired by Haskell's where clause, as posted to python-list by
Andrey Tatarinov. Alex Martelli suggested using the 'with' keyword.
statement ::= stmt_list NEWLINE | compound_stmt
statement ::= (stmt_list NEWLINE | compound_stmt) [local_namespace]
local_namespace ::= "with" ":" suite
Assignment statements (including augmented assignment) require special handling.
The original assignment statement is translated to a return statement in the
<target> <assign-op> <expr> with:
<target> <assign-op> unique_name()
Function and class definitions will also require special casing, so that the
created function or class is returned from the inner-scope and the name bound
correctly in the outer scope.
Note that calling locals() inside a statement with a local namespace will refer
to the statement's locals, rather than those of the containing function.
The original suggestion on python-list used the 'where' keyword. Alex
Martelli pointed out that this could be misleading for anyone with expectations
based on SQL's WHERE clause.
He suggested 'with' as an alternative spelling, as 'with' is already planned
as a keyword for Python3k. Even for with clauses associated with compound
statements, there should be no ambiguity, given that the with statement will
have an expression between it and the colon, while the with clause does not.
Is it actually possible to make it work?
Should the clause be allowed on any statement (as described), or restricted
to ones where it "makes sense"?
# Statement local functions (from Andrey Tatarinov)
# aka How to cope if lambda goes away :)
res = [ f(i) for i in objects ] with:
# Declaring properties (from Nick Coghlan)
x = property(get, set) with:
def set(self, value):
# Design by contract (from Nick Coghlan)
# Singleton classes (from Paul Rubin)
C = C() with:
# Complex default values (from Carlos Ribeiro)
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
More information about the Python-list