Const in python?

Dave Brueck dave at pythonapocrypha.com
Tue Mar 25 11:03:46 EST 2003


On Tue, 25 Mar 2003, David Brown wrote:

> > LIKE_THIS
> >
> > otherwise it is spelled
> >
> > likeThis
>
> There is just one little problem with ALL CAPS conventions - they look ugly,

To you, perhaps. This is a very widely used, understood, and accepted
convention - I quite like it.

> Conventions that make code obvious are generally a good thing, but ALL CAPS
> distracts the reader and changes the emphasis of the code, which is contrary
> to the spirit of Python

Not really - it's a cheap way to provide more insight into how the
developer sees the code and to convey more info to the reader. To me it
helps code readability quite a bit.

> Assigning to constants should not really be a problem - after all, you don't
> assign to variables unless you know what the variable is, and that the
> assignment is correct in the program.  Why treat constants any other way?

Well, one reason is that the value is only part of the information
associated with it. Another key piece of info is that you as the developer
do not consider, for example, MAX_FD to be a runtime fluctuating input to
your program.

But, if you don't like the convention, don't use it of course. Many, many,
many people do like and do use it though. At my current company we have a
very simple spelling standard the gist of which is:

MyClass, MyFunction, myVariable, MY_CONST

and we have yet to find that any of the spellings "distract" or "change
the emphasis" in some negative way - rather they convey more information
and improve readability.

> in Python, the run-time typing and binding means that you can make so
> many silly mistakes (it's the price you pay for the power and
> flexibility) that assigning to constants is a minor issue.

Oh, I defintely agree that assigning to constants is a minor issue - I
didn't bring this topic up, ya know. :) I use the ALL_CAPS spelling
because of the extra information it gives far more than as a safeguard
against accidental assignment, although the latter is still helpful.

-Dave





More information about the Python-list mailing list