Shoulid constants be introduced to Python?

ROGER GRAYDON CHRISTMAN dvl at psu.edu
Thu Nov 16 17:35:59 EST 2017


On Thu, Nov 16, 2017, Saeed Baig wrote:
>
Message: 7
>Date: Thu, 16 Nov 2017 17:16:11 +1100
>From: Saeed Baig <saeedbaig616 at icloud.com>
>To: python-list at python.org
>Subject: Should constants be introduced to Python?
>Message-ID: <5D4DA7B2-504A-4A3A-BACE-FFADEA1D25DF at icloud.com>
>Content-Type: text/plain;	charset=utf-8
>
>Hey guys I am thinking of perhaps writing a PEP to introduce constants to
>Python. Something along the lines of Swift?s ?let? syntax (e.g. ?let pi =
>3.14?).
>
>Since I?m sort of new to this, I just wanted to ask:
>- Has a PEP for this already been written? If so, where can I find the
>link/info to it?
>- Do you guys think it would be a good idea? Why or why not? Do you think
>there?s a better way to do it? I?d like to know what others think about this
>idea before making any formal submission.


Well, pi already does have a value:

>>> import math

>>> math.pi

3.141592653589793

but it's not a constant in the sense you are looking for:

>>> math.pi = 2

>>> math.pi

2



The only PEP I saw that makes any mention of constants is the PEP 8 Style Guide,
which recommends all constants be named with all caps, such as "math.PI".
(Why the math module doesn't do that beats me, unless it is there for
hyster^H^H^H^H^Historical reasons.)

But certainly if we saw code like this:

AVOGRADO_NUMBER = x + 2

I would believe that code to be 'obviously' since it is clearly attempting to
modify
a constant -- and the naming convention tells me so.

Do you have any particular reason you wish to promote constants?
Is it anything beyond "because C++ and Java, etc. have them"?

Because do note all of those constants in those languages are *declared*
to be constants, and declarations (esp variable declarations) are notably
absent.
I took a peek at the PEP 484 about Type Hints, and I didn't see anything there
about constants either.

So if you want to make a proposal, I think you should provide a compelling
argument in favor of a name change.   Suggesting that an interpreted language
have all the features of a compiled language just does not seem to be sufficient
cause.   After all:

Python pretends to have 'private' and 'protected' data, by having 'hidden'
variables with leading underscores, but they really are none of the above.

Python claims to have immutable data types, but those immutable data types
may contain mutable elements, which are indeed quite mutable.

Python does range checking on list indexes, but will not at all complain if
your code accidentally goes one step before [0] and ends up at [-1].

Python allows these loopholes in with the expectation that the programmer
should know better.   I think the same thing would be true with constants.

If you need this crutch to protect yourself from accidentally clobbering
constants,
then do better.   If you only need it to for documentation purposes, just tweak
the documentation.

Roger Christman
Pennsylvania State University




More information about the Python-list mailing list