Const in python?

David Brown david at no.westcontrol.spam.com
Tue Mar 25 16:49:12 CET 2003


"Dave Brueck" <dave at pythonapocrypha.com> wrote in message
news:mailman.1048602718.20497.python-list at python.org...
> 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.
>

Yes, I know it's a common convention - both in Python and in other
languages - and that many people use it.  It's a matter of personal taste, I
suppose.  I just tend to find that writing in all caps stands out too much,
and takes up too much visual space in the code.

I tend to use "MyClass, myFunction, myVariable, myConst" for Python, and
other high-level languages, but I'm not always strict about it.  For
assembly programming, I am strict about capitals for code, small letters for
data - it pays to be stricter there!


> > 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
>

Well, if it adds to the readability of your code - great!  I always like to
keep in mind that the computer will spend milliseconds reading the code,
while humans might be looking at it for hours, days, weeks or years, and
it's important to write accordingly.

David







More information about the Python-list mailing list