Creating a local variable scope.

Johan Grönqvist johan.gronqvist at gmail.com
Sat Sep 19 09:48:42 CEST 2009


Gabriel Genellina skrev:
> En Fri, 18 Sep 2009 10:55:47 -0300, Johan Grönqvist 
> <johan.gronqvist at gmail.com> escribió:
> 
>> Summarizing the answers, it seems that I will try to follow three 
>> suggestions:
>>
>> 3) If I define a few values intended to be used very locally, delete 
>> those after use.
> 
> Why bother? Unless they're big objects and you want to ensure they get 
> deleted as soon as possible.

To ease debugging.

Perhaps the problem only appears because I use longer functions than 
recommended for python, but I have functions containing 2 to 4 loops, 
with several if-clause each, where the different parts use very similar 
variable names, like x, y, z, dr, dr_2, dr_3 etc.

None of the code fragments uses all of the names, but every fragment 
uses some of them. I have had typos that incorrectly reused values from 
a previous fragment. In those cases, it would have been much easier to 
debug a raised exception due to using an undefined name, than to debug a 
slightly incorrect result due to some if-clause of some loop computing 
with an incorrect value.


Regards


Johan




More information about the Python-list mailing list