Creating a local variable scope.

markolopa marko.loparic at gmail.com
Mon Nov 30 03:50:04 EST 2009


Hi Steve!

On Nov 30, 1:46 am, Steve Howell <showel... at yahoo.com> wrote:
> I occasionally make the error you make, but I think the real problem
> you are having is lack of attention to detail.  If name collisions are
> a common problem for you, consider writing shorter methods

Yes, this could be a solution, though I believe that too many (sub)
methods called by a single method turn the code less readable.

> or develop
> the habit of using more descriptive variable names.

Also a good suggestion. Alternatively I have considered to do the
opposite, to use weird (the opposite of descriptive) names for
variables that should exist only inside the block, in order to
differentiate them from the variables that should persist.

>  In the code
> above, you could have easily cleaned up the namespace by extracting a
> method called get_arg_columns().  Having to spend 15 minutes tracking
> down a bug usually indicates that you are not being systematic in your
> thinking.  If you are rushing too much, slow down.  If you are tired,
> take a break.  If you make the same mistake twice, commit to yourself
> not to make it a third time.  Also, test your methods one at a time
> and get them rock solid before writing more code.

Nice suggestions thanks, all of them apply to me!...:-) Compared to
the normal coder I have a strong tendency to loose myself in details.
That is why I have to be very systematic, write lots of defensive code
(to catch the bugs as soon as possible), write unit tests (great habit
I've developped recently!). Python is fantastic for those goals. The
only laking feature I find is really the survival of variables, a
problem that does not exist in C++ or in Perl (with use strict).
Besides this kind of bug I have noticed only 2 other sources of bug I
had more than once with python code.
- expecting float from integer division (easily avoidable, either with
3.0 or from __future__...)
- expecting diferent objects in a list have having the same several
times (deepcopy issue, also easily avoidable once we know it)

So my only other problem is with this "survival" feature. I consider
to write my own version of Python, the one that would not allow the
usage of varaibles outside the block it was created. Of course I won't
rewrite CPython but I could add an error (or warning) to pylint. With
some luck it already exists, I will investigate...

Greets!
Marko



More information about the Python-list mailing list