Class or Dictionary?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 12 01:13:49 CET 2011


On Fri, 11 Feb 2011 10:15:27 -0800, Dan Stromberg wrote:


> I'd use a class rather than a dictionary - because with a class, pylint
> (and perhaps PyChecker and pyflakes?) should be able to detect typos
> upfront.  

*Some* typos. Certainly not all.

The more complex the code -- and with 100 or so parameters, this sounds 
pretty damn complex -- there is a non-negligible risk of mistakenly using 
the wrong name. Unless pylint now has a "do what I mean, not what I say" 
mode, it can't save you from typos like this:

params.employerID = 23
params.employeeID = 42
# much later
if params.employeeID == 23:
    # Oops, I meant employ*er*ID
    ...



> With a dictionary, typos remain runtime timebombs.

Are your unit tests broken? You should fix that and not rely on just 
pylint to detect bugs. Unit tests will help protect you against many more 
bugs than just typos.

Besides, and I quote...


"I find it amusing when novice programmers believe their main job is 
preventing programs from crashing. ... More experienced programmers 
realize that correct code is great, code that crashes could use 
improvement, but incorrect code that doesn't crash is a horrible 
nightmare." -- Chris Smith



-- 
Steven



More information about the Python-list mailing list