Behaviour of enumerated types (was: Re: Immutable instances, constant values)

Ben Finney bignose+hates-spam at
Fri Nov 18 13:43:10 CET 2005

Bengt Richter <bokr at> wrote:
> Ben Finney <bignose+hates-spam at> wrote:
> >I've recently packaged 'enum' in PyPI.
> [...]
> My notion of enum comes from (what I remember of) Pascal

You might want to investigate the 'enum' package for my idea of how an
enumerated type can work.

> which is basically an ordered set of names of integers forming a
> type, and ord(one_of_the_names) gets you the index value.

Getting a numeric index might be useful in a language such as Pascal,
with no built-in dict or sequence types. In Python, where any
immutable object can be a dict key, and any sequence can be iterated,
it seems of no use.

> But what we have is named integers, much as True and False are built
> in names for integer subtypes with value 1 and 0.

That's an implementation detail; surely code shouldn't be expecting
any particular relationship between integers and boolean values?

> So I'd say enums should also be int subtypes...

Likewise, that seems best left as an implementation detail. Why expect
any particular mapping to int values? Doing arithmetic or boolean
logic on enumerated values seems against their purpose.

 \      "We tend to scoff at the beliefs of the ancients. But we can't |
  `\        scoff at them personally, to their faces, and this is what |
_o__)                                      annoys me."  -- Jack Handey |
Ben Finney

More information about the Python-list mailing list