Should constants be introduced to Python?
bartc
bc at freeuk.com
Thu Nov 16 19:26:35 EST 2017
On 16/11/2017 06:16, Saeed Baig wrote:
> 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.
>
I've taken part in a few discussions here on the subject.
However, if you're going to write a PEP, does that require that you have
some idea of how it would be implemented?
For example, you write this:
let A = 10
let B = 20
x = A + B
On CPython, would the byte-code compiler reduce the A+B to 30, or would
it still do the calculation?
Suppose those lets were in an imported module M:
import M
x = M.A + M.B
Would it reduce the expression here or not? (And if so, how? Remember
that M itself is not constant, so M could have been reassigned as
another module, or a class, so that another M.A is not a constant.)
What would happen here:
let A = 10
A = 12
Would the assignment be allowed or not? Because the way Python works
now, is that EVERY (top-level) identifier created by the user is a
variable, and can be assigned something else. To prohibit A=12 would
mean creating a new category of identifier that is fixed.
And the error would be hard to pick up at compile-time here:
M.A = 12
As the category of M.A is not known to the byte-code compiler (also that
M is not constant as I said).
Note that names are created also with 'import', 'def' and 'class', and
it might be desirable to have these constant too, for example:
let def F():
--
bartc
More information about the Python-list
mailing list