c enum - how to do this in python?
Dan Bishop
danb_83 at yahoo.com
Fri Feb 21 22:25:11 EST 2003
Rene Pijlman <reageer.in at de.nieuwsgroep> wrote in message news:<haad5voagh78aempcuv3g460p4f2eea3ne at 4ax.com>...
> Klaus Meyer:
> >>> enum { M0, M1, M2, MCOUNT };
> >>> How to do this in Python?
> >>
> >> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67107
> >
> >Thanks, but surprisingly complex in Python...
>
> I agree, but compare Java, just for fun...
> http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
>
> I'm not sure where things went wrong in computer science.
>
> C lacks a byte data type, even though it basically treats
> everything as a contiguous array of bytes. Python is a high
> level programming language including bolean logic, but until
> recently it lacked a boolean datatype. enum is a basic concept
> in a primitive language like C, but it needs to be emulated with
> unreasonably complex constructs in high level languages like
> Java and Python.
But the HLL enum isn't comparable to the C enum.
To get the semantics of a C enum in Python, all you have to do is
# typedef enum {...} season;
winter, spring, summer, autumn = range(4)
season = int
But HLL programmers want to treat enums differently from ints, e.g.,
having str(winter) == 'winter' instead of '0', not allowing
multiplication, etc. Obviously, this leads to complex code.
C programmers just live with the weakly-typed enums.
> Strings are immutable and constants are not.
This isn't an inconsistency; you're confusing objects and variables.
More information about the Python-list
mailing list