Statement local namespaces summary (was Re: python3: 'where' keyword)
Nick Coghlan
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).
Cheers,
Nick.
Abstract
--------
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::
<statement> with:
<suite>
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.
Grammar Change
--------------
Current::
statement ::= stmt_list NEWLINE | compound_stmt
New::
statement ::= (stmt_list NEWLINE | compound_stmt) [local_namespace]
local_namespace ::= "with" ":" suite
Semantics
---------
The code::
<statement> with:
<suite>
translates to::
def unique_name():
<suite>
<statement>
unique_name()
Assignment statements (including augmented assignment) require special handling.
The original assignment statement is translated to a return statement in the
inner scope::
<target> <assign-op> <expr> with:
<suite>
translates to::
def unique_name():
<suite>
return <expr>
<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.
Keyword Choice
--------------
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.
Open Issues
-----------
Is it actually possible to make it work?
Keyword choice
Should the clause be allowed on any statement (as described), or restricted
to ones where it "makes sense"?
Examples
--------
# Statement local functions (from Andrey Tatarinov)
# aka How to cope if lambda goes away :)
res = [ f(i) for i in objects ] with:
def f(x):
#do something
# Declaring properties (from Nick Coghlan)
class C(object):
x = property(get, set) with:
def get(self):
pass
def set(self, value):
pass
# Design by contract (from Nick Coghlan)
@dbc(pre, post)
def foo():
pass
with:
def pre():
pass
def post():
pass
# Singleton classes (from Paul Rubin)
C = C() with:
class C:
pass
# Complex default values (from Carlos Ribeiro)
def f(x=default()):
pass
with:
def default():
pass
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list