[Python-ideas] Should Python have user-defined constants?
Steven D'Aprano
steve at pearwood.info
Tue Nov 21 19:27:59 EST 2017
On Tue, Nov 21, 2017 at 09:34:33AM -0800, Chris Barker wrote:
> On Tue, Nov 21, 2017 at 3:31 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:
> > The
> > interesting (but is it useful?) concept is constant identifiers which
> > cannot be re-bound or re-assigned once they are set the first time.
>
> This would actually be a substantial shift in what Python is about --
> currently the concept of type is all about values, not names
I didn't say anything about types. My comments were about names and
values (objects):
- Mutability is a property of values (objects): some objects are
mutable, and other objects are not.
- Constantness is a property of names or other identifiers: some
names can only be bound once ("constants"), other names can be
bound and rebound at will ("variables").
Types are literally irrelevant to this, except in the sense that in many
languages, including Python, the distinction between mutable and
immutable is usually controlled by the type of object. But that's not
fundamental to the concept: we could, if we wanted, decouple the two.
> -- there are
> various rules about scope, and they can be adjusted with global and
> nonlocal, but it's only about scope -- a name is still a name, and can be
> bound to any type of object, etc.
Sure, that's the way Python is now. But that's not fundamental to the
concepts. Other languages allow other distinctions between names, not
just scope: for instance, names can be static (they keep their value
between function invocations), they can be read-only some languages
allow you to specify whether they are kept in RAM or in a register.
> static languages, on the other hand often (always) assign type to the name
> itself -- so adding constants and the like makes sense.
Constantness is orthogonal to the concept of static typing of variables.
> But changing the
> property of a name (this name can not be rebound) is a big shift in what
> names are about in Python -- and I don't think a good one.
It is a big shift, although not that big since we already have
read-only properties. This would "merely" (hah!) extend that concept to
other namespaces apart from instances.
I'm undecided as to whether the benefit would justify the cost.
--
Steve
More information about the Python-ideas
mailing list