Creating a local variable scope.

Ethan Furman ethan at stoneleaf.us
Fri Sep 11 21:42:49 CEST 2009


Patrick Sabin wrote:
> Johan Grönqvist schrieb:
> 
>> 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])
>> ----------

[snip]

> 1) If you define all this in the global namespace you could remove your 
> temporary variables afterwards, e.g.
> 
> spam = 1
> ham = (spam,)
> del globals()['spam']

Why use globals()?  You could just say

del spam

and be done with it.

~Ethan~



More information about the Python-list mailing list