The Industry choice

Roy Smith roy at panix.com
Sun Jan 2 09:06:33 EST 2005


Stefan Axelsson <crap1234 at hotmail.com> wrote:
> Yes, ignoring most of the debate about static vs. dynamic typing, I've 
> also longed for 'use strict'.

You can use __slots__ to get the effect you're after.  Well, sort of; it 
only works for instance variables, not locals.  And the gurus will argue 
that __slots__ wasn't intended for that, so you shouldn't do it.

> Sure Python isn't as bad as (say) Awk in this respect; you have to at 
> least assign a variable to make it spring into existence

I think you've hit the nail on the head.  In awk (and perl, and most 
shells, and IIRC, FORTRAN), using an undefined variable silently gets 
you a default value (empty string or zero).  This tends to propagate 
errors and make them very difficult to track down.

In Python, you raise NameError or AttributeError, so you find out about 
your mistake quickly, and you know exactly where it is.  The only time 
you can really go wrong is when you've got multiple assignment 
statements with the same lhs and you make a typo in one of them.  And 
even then, as you say, things like Pychecker will probably catch the 
mistake.

In perl, I always use "use strict", but in Python, I just don't feel the 
need.  Between the exception mechanism and unit tests, the odds of a 
typo going unnoticed for very long are pretty slim.  I'll admit I don't 
use Pychecker, but if I was doing production code, I would probably use 
it as part of my QA process.

> I don't think that anyone could argue that typing 'use apa' before 
> the first actual use (or words to that effect) would 'slow them 
> down', or be very onerous.

Well, I'll have to (respectfully) disagree with you on that.  It's not 
that there's no value in explicit declarations, it's just that (IMHO) 
the cost exceeds the value, given the other tools we have in Python to 
catch the mistake.



More information about the Python-list mailing list