constant in python?

Alex Martelli aleaxit at yahoo.com
Sat Aug 18 03:54:35 EDT 2001


"liam" <lcampbell at ubytes.com> wrote in message
news:sqff7.779$2k4.75679 at news.pacbell.net...
> i found a similar example on the net i was just making sure. seems kinda
> alot of work just to get a constant. even perl has "use constant" and
thats
> about the ugliest language around :)
    ...
> > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207

Note that the purpose of the work is ensuring an exception gets
raised if one accidentally tries to rebind a module-attribute that
is meant to be 'constant' (read-only).  "to get a constant", i.e.,
an attribute that does not change, you have to do nothing special:
just don't rebind it, and it will not change.

I agree that in most cases it's not worth it -- it's not important
enough to ensure exceptions get raised for such programming
errors, as an exception will be raised soon afterwards if you run
decent tests AND it's actually important for the attribute not to
be re-bound (often you'll find there's no real need for the
'constantness' and the program works fine even if things do
change:-).  The real purpose of recipe 65207 is showing how
to use an instance in lieu of a module and thereby gain the
ability to use __getattr__/__setattr__ on it.

But if it's not important enough to raise exceptions on such
errors, for it to be worth including a few lines of code in your
site-configuration script, how can it POSSIBLY be important
enough to be worth making the language itself bigger and
more complicated?!  ANY feature in a language must pull its
own weight, else you end up with huge, redundant languages.

If something can be done with a few lines of code in the
language as it is, it requires very strong motivation for that
something to become a built-in language mechanism: most
particularly, that something must be a very important task,
frequently performed, and with a substantial advantage (in
terms of ease of use, performance, or simply the pervasive
effects of standardization) to have it in the language itself.

I think few languages have followed these guidelines for
design and evolution as closely as Python -- not that Python
has been perfect about it, of course, but quite reasonably
close to that ideal.  I think that "constants" are a good
example of something that should NOT be in the language
itself, exactly because it's a minor issue and it can be
easily implemented without too much trouble (it could
be placed in the standard library, which has somewhat
laxer requirements than the language proper, but that's
pretty iffy too).  Good examples of things that _are_
eventually added to the language include the list
comprehensions added in 2.0, nested scopes added in
2.1, iterators added in 2.2.  THESE are language features
that pull their own weight (and even on this you will
find disagreement -- a number of practising pythmen are
rather minimalists than pragmatists:-).


Alex






More information about the Python-list mailing list