[Tutor] Enumeration and constant
Alan Gauld
alan.gauld at freenet.co.uk
Mon May 29 17:43:32 CEST 2006
> I wander how to setup the enumeration type
> and constant in Python,
These concepts do not exist in Python per se.
The convention is to make constant names uppercase:
PI = 3.1415926
Enums are a little more variable in implementation depending
on what you want to do with them. If you need to iterate over
them a tuple might be a choice with strings as values. But if
you need to use them as indices into a list a dictionary might
be better.(But you could use them as keys into a dictionary
instead of course!)
Other more sophisticated options include creating classes.
These can implement restricted ranges etc
In practice I rarely find I need enums in Python because the
wealth of data types (lists, tupoles, sets, dictionaries, classes)
means I rarely need and extra symbolic values. I usually find
myself using enums in other languages because I don't have
quite the right data type available and an enum helps fake it.
HTH,
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list