
Hello,
On Tue, 25 May 2021 11:53:20 -0000 "Shreyan Avigyan" pythonshreyan09@gmail.com 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:
https://www.mail-archive.com/python-dev@python.org/msg110424.html "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" (https://www.mail-archive.com/python-ideas@python.org/msg25403.html) feature of my Python dialect, Pycopy (https://github.com/pfalcon/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 ===
Gives:
==== In import-time Warning: strict mode: overriding (monkey-patching) const name 'a' In run-time Traceback (most recent call last): File "strict.py", line 9, in __main__ RuntimeError: strict mode: cannot override const name ====