[Tutor] Python Version of "enum" ?

McLaughlin, Toby tmclaughlin at csu.edu.au
Fri Oct 22 04:31:59 CEST 2004


Perhaps this:

"Unpacking assignment also gives rise to another common coding idiom in
Python: assigning an integer series to a set of variables:

>>> red, green, blue = range(3)
>>> red, blue
(0, 2)

This initializes the three names to integer codes 0, 1, and 2,
respectively (it's Python's equivalent of enumerated data types you may
have seen in other languages)."

>From "Learning Python, 2nd Edition" by David Ascher & Mark Lutz.

Toby McLaughlin

> -----Original Message-----
> From: tutor-bounces at python.org 
> [mailto:tutor-bounces at python.org] On Behalf Of Gooch, John
> Sent: Friday, 22 October 2004 6:47 AM
> To: 'tutor at python.org'
> Subject: [Tutor] Python Version of "enum" ?
> 
> 
> Is there an equivalent of C++'s 'enum' keyword to set a 
> number of named
> constants ( like so ) 
> enum {
> 	OFF,
> 	ON,
> 	UNKNOWN
> }; 
> 
> in Python? It makes reading the code a lot simpler, as 
> opposed to having to
> look up what a status of "1" means, it can be replaced with 
> "ON", which is
> much easier to understand. 
> 
> If not, can you declare named constants another way? Perhaps 
> 'const MONDAY =
> 1', for example. 
> 
> Thank You, 
>  
> 
> John A. Gooch
> Systems Administrator
> IT - Tools
> EchoStar Satellite L.L.C.
> 9601 S. Meridian Blvd.
> Englewood, CO  80112
> Desk: 720-514-5708 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list