[Python-Dev] Lexical scoping in Python 3k

Nick Coghlan ncoghlan at gmail.com
Sun Jul 2 05:26:48 CEST 2006


Neil Schemenauer wrote:
>   The := would assign but not declare a variable
> in the current scope.

There are other benefits to such a statement, too, since we can make it 
similar to other augmented assignments by letting the object being assigned to 
interfere with the process.

   a := 2

could translate to something like:

   a = a.__assign__(2)

with the default behaviour of __assign__ simply being:

   def __assign__(rhs)
       return rhs

This gives you:
   - runtime checking for typos (you can't accidentally declare a new variable 
with := when you really meant to assign to an existing one)
   - if/when control flow analysis is added to the AST compiler, it will be 
picked up as an error at compile time along with the other augmented assignments
   - the object being assigned to can validate/modify its replacement (e.g. 
automatically wrapping it in a weakref proxy, or checking that it has the 
correct type)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list