[Tutor] Enumeration and constant

w chun wescpy at gmail.com
Tue May 30 01:53:41 CEST 2006


> > how about custom constant type?
> > Does python provide?
> No. Python tries to be as "light" as possible. There is a convention
> amongst Python programmers to use upper case names to represent
> constants, e.g. RED = 3.

another aspect of Python numbers that make them "constant" is that all
numeric types are immutable, meaning you cannot change their values
anyway.  the idiom that Bob and Alan described is mostly for the
programmer.  you can, of course, reassign that variable to another
number (which is also immutable), and Python won't stop you.

i have used tuples/lists and dicts as a proxy for enum-type
functionality, and they work just fine.

also, do not get confused between all this and the enumerate()
built-in function.  all that does is for a sequence, return both an
index and the corresponding sequence element so that folks stop doing
stuff like "for i in range(len(seq))".

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list