Creating a local variable scope.

Neal Becker ndbecker2 at gmail.com
Fri Sep 11 14:08:35 EDT 2009


Johan Grönqvist wrote:

> Hi All,
> 
> I find several places in my code where I would like to have a variable
> scope that is smaller than the enclosing function/class/module definition.
> 
> One representative example would look like:
> 
> ----------
> spam = { ... }
> eggs = { ... }
> 
> ham = (a[eggs], b[spam])
> ----------
> 
> The essence is that for readability, I want spam and eggs in separate
> definitions, but for clarity, I would like to express the fact that they
> are "local to the definition of ham", i.e., they are not used outside of
>   the definition of ham.
> 
> The language reference at
> <http://docs.python.org/reference/executionmodel.html> says that "The
> following are blocks: a module, a function body, and a class
> definition." (all other cases seem to refer to dynamic execution using
> eval() or similar). Python 3 and 2.6 seem to have identical scope rules.
> 
> In the other languages I have used I can either use braces (C and
> descendants) or use let-bindings (SML, Haskell etc.) to form local scopes.
> 
> Are there suggestions or conventions to maximize readability for these
> cases in python? (Execution time is not important in the cases I
> currently consider.)
> 
I'd like this also.




More information about the Python-list mailing list