Creating a local variable scope.

r0g aioe.org at technicalbloke.com
Mon Nov 30 23:20:37 EST 2009


markolopa wrote:
> On Nov 30, 4:46 am, Dave Angel <da... at ieee.org> wrote:
>> markolopa wrote:
<snip>
>> or
>> whether you accidentally reused the same name without giving it a new
>> value in the new loop.  
> 
> That is what I do regularly...8->
> 



Well really dude, you need to stop doing that. It's not a language
problem, it's a memory problem (human not RAM!). You need to be aware of
what you're putting into your namespace. This is one of the reasons
people try to avoid using global variables, if you forget you've used a
variable name and use it again somewhere you get an undefined state and
hours of debugging / data loss / subtle corruptions you won't spot until
 the backups have all been cycled etc.

I'm surprised you have a recurring problem with this really. My memory
is terrible and I reuse variable names all over the place without any
trouble. Maybe try keeping the length of your functions down so that
they fit on a single screen and create and use a strong naming
convention i.e.

Long, descriptive variable names all_in_lower_case
Function names all in CamelCase
Global names are in ALL CAPS
Loop variable names all prefixed with 'each_'

The details don't so much matter as long as you are consistent with it.
I'm sure many here would hate my coding conventions if they had to use
them but they seem to work OK for me and I can't recall ever having this
problem.


Roger.



More information about the Python-list mailing list