Const in python?

Bjarke Dahl Ebert bjarke-news2003 at trebe.dk
Mon Mar 24 16:49:08 EST 2003


"Anand" <anand at calbay.com> wrote in message
news:c6303995.0303241023.4d106ab6 at posting.google.com...

> Wouldn't it be a nice idea to have constants in Python? I think when
> programmers intend some of the variables to be const, the behavior
> shouldn't be allowed to be altered. It would be nice to declare
> 'maxint' in sys to be a const rather than allowing programmers to
> change the value and cause the program to fail as a result of this
> change.

Being constant in Python doesn't mean the same thing as e.g. in C++.
In Python, all numbers and strings are already constant (immutable).

So, allowing this:
   a = 1
   a = 2
is really not a property of the "object 1" or "object pointed to by a", or
even "a", but rather a property of "module objects". The "module object"
allows you to rebind the name 'a' to mean something different from what it
meant before.

It's like this:

before:
a ----> 1

after:
   (--->) 1
a ----> 2


So a relevant discussion could be that of r/w or r/o state of attributes in
objects (especially module objects).
Which attributes of the sys module should be constant, one could ask

While this is a valid discussion, I tend to feel that the current situation
works quite well.
Nobody changes attributes in 'sys' without good reason. Of course, the
programmer would like to know that module Foo changes sys.stdout, for
instance.

Importing some win32all-module (pythonwin?) will actually change sys.stdin,
so that input comes from a popup dialog. I actually had to change some
program to backup sys.stdin before importing the win32-stuff, then set it
back ;-)

Bjarke








More information about the Python-list mailing list