Statement local namespaces summary (was Re: python3: 'where' keyword)

Nick Coghlan ncoghlan at iinet.net.au
Tue Jan 11 05:30:29 EST 2005


Andrey Tatarinov wrote:
> I think using 'with' keyword can cause some ambiguity. for example I 
> would surely try to write
> 
>  >>> x = a+b with self:
>  >>>     b = member
> 
> and using with at the end of block brings more ambiguity:
> 
>  >>> stmt1()
>  >>> stmt2()
>  >>> with self:
>  >>>     member = stmt3()
> 
> compare to:
> 
>  >>> stmt1()
>  >>> stmt2()
>  >>> with:
>  >>>     variable = stmt3()
> 
> a way different semantics with just one word added/deleted.

Except that for a "with <expr>:" block, attributes of the expression must be 
preceded by a dot:

Py> stmt1()
Py> stmt2()
Py> with self:
...    .member = stmt3()

The advantages of the 'with' block are that 'self' is only looked up once, and 
you only need to type it once. The leading dot is still required to disambiguate 
attribute references from standard name references.

Despite that, I think you are right that the ambiguity is greater than I first 
thought. Correct code is reasonably easy to distinguish, but in the presence of 
errors it is likely to be unclear what was intended, which would make life more 
difficult than it needs to be.

However, I still agree with Alex that the dual life of "where" outside of Python 
(as an 'additional definitions' clause, as in mathematics, and as a 
'conditional' clause, as in SQL), and the varied background of budding 
Pythoneers is a cause for concern.

'in' is worth considering, as it is already used by Python at least once for 
declaring use of a namespace (in the 'exec' statement). However, I suspect it 
would suffer from ambiguity problems similar to those of 'with' (consider 
"<expr> in <expr>" and "<expr> in: <expr>"). There's also the fact that the 
statement isn't *really* executed in the inner namespace - any name binding 
effects are seen in the outer scope, whereas 'exec x in dict' explicitly 
protects the containing namespace from alteration.

So of the four keywords suggested so far ('where', 'with', 'in', 'using'), I'd 
currently vote for 'using' with 'where' a fairly close second. My vote goes to 
'using' because it has a fairly clear meaning ('execute the statement using this 
extra information'), and doesn't have the conflicting external baggage that 
'where' does.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list