On Sun, Nov 15, 2020 at 03:18:29PM +0300, Paul Sokolovsky wrote:
amount of work to implement pattern matching is certainly an order of magnitude larger than to introduce constants [...]
[Steve]
Here's a toy proposal, []
The baseline of my version is much simpler:
# This makes "const" a kind of hard keyword for this module from __future__ import const
FOO: const = 1 # obviously, this is constant Foo_Bar: const = 2 # this is too foo: const = 3 # and like it or not, but this is constant either
"const" annotation is recognized by compiler (scope resolving component, aka symtable builder to be exact) as a property orthogonal to localness/globalness. Literally, "symtable" module (https://docs.python.org/3/library/symtable.html#symtable.Symbol) will grow Symbol.is_const() method.
Oh, well, if all it takes is to add a new keyword, then constants are easy! No need to worry about how constantness affects name resolution, or how the syntax interacts with type-hints: spam: const: Widget = build_widget(*args) # Maybe this is better? # let spam: Widget = build_widget(*args) or how "constant" interacts with mutability: spam: const = [] spam.append(99) # Allowed? spam.extend(items) spam.sort() or for that matter, whether or not constants are actually constant. spam: const = 1 spam = 2 If constants aren't actually constant, but just a tag on symbols, then you would be right, it probably would be trivially easy to add "constants" to Python. But I think most people agree making them behave as constants is a pretty important feature. *The* most critical feature of all, really. Given a declared constant: # Module a.py x: const = None how do you prevent not just code in module `a` from rebinding the value: x = 1 globals()['x'] = 2 exec('x = 3', globals()) but other modules as well: # Module b.py import a a.x = 'spam' vars(a)['x'] = 'spam' exec('x = 3', vars(a)) -- Steve