How do you create constants?

Quinn Dunkan quinn at retch.ugcs.caltech.edu
Thu Nov 2 06:17:13 CET 2000


On Tue, 31 Oct 2000 11:10:25 +0100, Alex Martelli <aleaxit at yahoo.com> wrote:
>"Mikael Olofsson" <mikael at isy.liu.se> wrote in message
>news:XFMail.001031081500.mikael at isy.liu.se...
>> Can someone tell me why anyone would need true constants?
>
>To give a "readable name", emphasizing their meaning,
>to what one could otherwise express in code as (e.g.) a
>number.

You didn't answer his question.  I believe his question was "Can't you just
bind a name and then not rebind it?  If it's easy to just not rebind names,
why do we need a special language construct?"

You answer sounded like: "So you can bind a name, and not worry about it
getting accidentally clobbered."

The real question is "what do you mean by 'accidentally clobber'?"  I believe
he was looking for ways you accidentally type 'math.pi = 3'.  I'm interested
in those ways, too, since I can't think of any.  But maybe they're there on
certain kinds of projects.

>after all.  For the common use of `need` as `would
>be quite convenient`:-), doubting the `need` for
>named constants may be myopic.  "If you don't have
>a hammer, no problem looks like a nail" syndrome?-)

That's assuming everyone here speaks *only* python.  Before I knew python, I
used several other languages that had names that could be marked as 'not for
rebinding' (haskell certainly fulfills that, but that doesn't count :).  When
using those languages, I use readonly attrs, immutable classes, etc. for
documentation and perhaps some (imaginary?) efficiency.  But I'm not sad to
see them go in python.  Documentation is achieved by 'const.foo' or some other
convention and efficiency was never an issue in the first place for python.
Type is a seperate issue: in referentially opaque languages, static typing has
caught a number of aliasing bugs for me, but not very many.  Explicit
constants have caught zero bugs.

So I've got plenty of hammers.  That thar's a wooden screw :)

But I have little experience with many kinds of projects.  My mind could
change.

I'm just mentioning this because, due to some strange convergence of the
earth's magnetic fields, there have been a whole bunch of threads in here
about 'constants' and 'immutable dicts' and whatnot, so I'm curious.



More information about the Python-list mailing list