Now it seems Python doesn’t need constant. There are many ways we can achieve constants. Though constants may increase performance. Yet again it can also be the opposite.


From: Paul Sokolovsky <>
Sent: Saturday, May 29, 2021 12:44:09 AM
To: Shreyan Avigyan <>
Cc: <>
Subject: [Python-ideas] Re: Introduce constants in Python (constant name binding)

On Tue, 25 May 2021 11:53:20 -0000
"Shreyan Avigyan" <> wrote:

> I posted my previous idea regarding this on the mailing list. This
> idea is a little different. This idea suggests introducing constant
> name bindings. This is similar to const pointer in C/C++. Once a name
> has been assigned to a data we can change the data (if mutable) but
> we cannot change the name to point to a different data. The only way
> the data the constant points can get deallocated is if it goes out of
> scope, the program exits or the constant is manually `del` by the
> user. The proposed syntax is as follows,
> constant x = 10
> constant y = ["List"]
> constant z: str = "Hi"

The idea of introducing constants on the core language level in Python
is well-known topic and was brought up on multiple occasions both on
the python-ideas and python-dev mailing lists. A random example:
"constants in Python: Starting simple and gradually adding more
features, was: Re: Pattern Matching controversy"

The matter is actually implementing it in different Python
implementations. And some implementations had support for *some kind*
of constants for many years (e.g. MicroPython with it's pre-annotation
syntax of "FOO = const(1)"), while CPython still has it only on the
level of external first-generation annotation module, "typing".

As another example, I can give "strict mode"
feature of my Python dialect, Pycopy
(, which implements some (but again,
not all) aspects of const'ness. E.g.:

print("In import-time")
a: const = 1
a: const = 2

def __main__():
    print("In run-time")
    global a
    a = 3


In import-time
Warning: strict mode: overriding (monkey-patching) const name 'a'
In run-time
Traceback (most recent call last):
  File "", line 9, in __main__
RuntimeError: strict mode: cannot override const name

Best regards,
Python-ideas mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct: