Const in python?

Dave Brueck dave at pythonapocrypha.com
Mon Mar 24 16:15:13 EST 2003


On Mon, 24 Mar 2003, Anand wrote:

> Wouldn't it be a nice idea to have constants in Python?

IMO, no. (BTW, this has come up many times before - check the Google
archives).

> I think when programmers intend some of the variables to be const, the
> behavior shouldn't be allowed to be altered.

When I intend a variable to be constant it is spelled

LIKE_THIS

otherwise it is spelled

likeThis

This works very well in practice and I highly recommend it - no problems
yet! :)

> It would be nice to declare 'maxint' in sys to be a const rather than
> allowing programmers to change the value and cause the program to fail
> as a result of this change.

I disagree. Assume you have a variable SOME_CONST. By it's spelling I can
see that it is constant, so changing it is a no-no. Now, if I go and
change it anyway, then one of these is probably true:

#1 I know what I'm doing, and realize that changing it might be dangerous,
but for some reason I need to change it anyway (e.g. I'm debugging a
problem in a production system and changing this value will really help me
out of a jam).

#2 I am a sloppy programmer and lack the discipline to follow coding
standards, good practicies, common conventions, etc.

#3 I'm ignorant of the "all caps means constant" convention.

If #1 is true, then not being able to change the "constant" is a real
bummer, and really annoying. If #2 is true, then my sloppiness will be
manifest in so many other areas that changing constants will be the least
of my problems, and I'll weasle my way around all sorts of "best practice"
fences you put up anyway. If #3 is true, it's trivial to educate me.

At least in this respect (and I suppose others, like "private" members of
classes), Python caters to the programmer in #1. Thank goodness it does
not cater more towards the #2 programmer (something that would be
especially annoying to the #1 type of programmers) it's no fun to be
restricted because _somebody else_ is a bozo.

-Dave





More information about the Python-list mailing list