[Python-ideas] Should Python have user-defined constants?

Victor Stinner victor.stinner at gmail.com
Tue Nov 21 10:47:05 EST 2017

2017-11-21 7:33 GMT+01:00 Saeed Baig <saeedbaig616 at icloud.com>:
> Hey guys I am thinking of perhaps writing a PEP to introduce user-defined
> constants to Python. Something along the lines of Swift’s “let” syntax (e.g.
> “let pi = 3.14”).

If you want to work on a PEP, you will have to write a strong
rationale for 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 (I’ve already posted this same
> question on python-list, but I just wanted to gauge opinion here too).

Python has different kinds of namespaces: module globals, class
attributes, function local variables, etc.

The https://github.com/fijal/quill programming language looks like
Python but makes module globals *mapping* "immutable": setattr(module,
'var', new_value). Only the mapping is immutable, a value can be
mutable. I guess that the motivation here is to help the optimizer to
emit more efficient code.

See also previous attempts:

"PEP 416 -- Add a frozendict builtin type"
=> my motivation was to develop a sandbox for Python

"PEP 351 -- The freeze protocol"
=> I guess that the main motivation was to previous programming
mistakes, misuse of an API

The question is if you only want to have a technical solution to
prevent modification of module globals, or if you would like to
advertize that a variable is constant and use it somehow.


More information about the Python-ideas mailing list