equivalent of enum?

Jesse Sweeney jesse at student.usyd.edu.au
Tue Sep 14 10:37:43 EDT 1999


On Tue, 14 Sep 1999 14:16:03 +0100, Ian Clarke <I.Clarke at strs.co.uk> wrote:

>
>> In more direct answer to your question, there is no enum type in Python.
>> It's simple enough to declare objects that you treat as constants, however:
>
>I have always been a little dissatisfied with the way enums are normally
>implemented, namely with an integer.  Why not make an "enum collection"
>type (I am sure there must be a better name).  In essence this would be
>an object which had several enums, say:
>
>Sex: (male | female)
>employed?: (yes | no)
>os: (linux | windows | macos)
>
>etc.  Normally these would be treated as 3 enums and would be stored as
>three integers - 3 X 32 bits, or 96 bits, yet you can actually store
>this data using just 4 bits (and that would contain a little bit of
>redundancy).  The first bit would determine male or female, the second
>bit employed or unemployed, and the third and fourth bits would store
>the os.
>
>In practice this could be implemented by creating an enum class.  We
>could then create a function which takes an object containing enums (and
>possibly allow these to be mixed with other data types) and compresses
>the data bit by bit.  In the end we would get really efficient data
>storage.  

This is the equivalent of a C struct with bit fields (and I assume you could
implement it that way for a Python extension type). However, the general
consensus[1] over in C-land is that using bit fields is usually not a great
idea, or at least usually a misguided one. The reason being that although the
data space is reduced, the size of the code generally increases. Also, since
integers conveniently fit so nice and snugly into CPU registers, accessing
fields in C is typically a fair bit slower than accessing the separate ints or
chars.

	Cheers, Jesse.


[1] Offtopic trivia of the day: I recently learned that 'consensus' is the most
frequently misspelled word in the English language. 'Brunet' comes in second.




More information about the Python-list mailing list