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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Nov 21 16:53:59 EST 2017

Steven D'Aprano wrote:
> In C, it might be something similar to:
> const int *a = &x;

Not quite, that makes whatever a points to constant, not a
itself. But you can do this:

     typedef int *ip;
     int x, y;
     const ip p = &x;

     void f(void) {
         *p = 42; /* ok */
         p = &y; /* not ok */

Compiling this gives:

% gcc -c constptr.c
constptr.c: In function ‘f’:
constptr.c:7: error: assignment of read-only variable ‘p’

BTW, this shows that gcc thinks of const-declared things
as "read-only variables" rather than constants. Which is
a bit of a silly term when you think about it!


More information about the Python-ideas mailing list