New statement proposal for Python

John Roth johnroth at ameritech.net
Fri Jun 15 00:11:54 CEST 2001


"Magnus Lie Hetland" <mlh at idi.ntnu.no> wrote in message
news:9gatpj$4vr$1 at tyfon.itea.ntnu.no...
> "David LeBlanc" <whisper at oz.nospamnet> wrote in message
> news:9gao5s$8ft$7 at 216.39.170.247...
> > One thing that bugs me about Python is that there's no really good way
to
> > have named constants so that magic numbers can be avoided. Assigning to
a
> > variable that (should) never changes is not the same (i.e. the "should
> > never" part).
> >
> > My proposal:
> >
> > alias httpport : 80
>
> Hm. Unconventional use of the colon, isn't it? (For Python, I mean.)

I don't see the problem with the colon - after all, isn't that the syntax
for
a key: value pair that will go into the dictionary? I'd just as soon have it
called "const" rather than "alias", though.

> Anyway - what is it you want with this?

The reasons I want it are very simple:
  1. it avoids coding errors when you have to have the same string
      constant in the code in multiple places. It's a lot easier for the
      compiler to complain about an uninitialized variable than to check
      lots of code with Eyeball Mark I.
  2. Sometimes the "magic number" just isn't really very descriptive.
      Picking good names for things is part of what makes your module
      understandable six months from now.

>Efficiency, on enforcing
> the non-changing state of the variables?

If I call something "const", then I want it to be CONSTANT, not
something that can be changed at the whim of a coding mistake, or a
misguided "cute hack."

>
> I can't really see the need for this myself - but if something like it is
> ever implemented, I really hope we won't have similar solutions to those
> used to simulate private variables. I mean...
>
> >>> _const_httpport = 80
> >>> _const_httpport = 90
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ConstError: constant '_const_httpport' is already defined
>
> <shudder>
>
> > Dave LeBlanc
>
> --
>
>   Magnus Lie Hetland         http://www.hetland.org
>
>  "Reality is that which, when you stop believing in
>   it, doesn't go away."           -- Philip K. Dick
>
>
>





More information about the Python-list mailing list