Behaviour of enumerated types

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Nov 18 19:10:42 EST 2005


Bengt Richter <bokr at oz.net> wrote:
> Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> >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.
>
> Does your concept of enumeration not have a fixed order of a set of
> names?

It does. The values are iterable in the same order they were specified
when creating the Enum.

> If it does, what is more natural than using their index values as
> keys to other ordered info?

I don't see why. If you want sequence numbers, use enumerate(). If
not, the enum object itself can be used directly as an iterable.

> OTOH, the index values (and hence my enums) are[1] not very good as
> unique dict keys, since they compare[2] promiscuously with each
> other and other number types.

Indeed, that's why (in my design) the values from the enum are only
useful for comparing with each other. This, to me, seems to be the
purpose of an enumerated type.

> To me the int correspondence is as expectable and natural as
> a,b,c=range(3) (at least as a default) though I think different
> enumerations should be different types.

That's a complete contradiction, then. If you want them to be
different types, you don't want them to be integers.

> Note that the ordering of int values makes the instances nicely
> sortable too, e.g.,

That only means the enum values need to compare in the same sequence;
it doesn't mean they need to correspond to integer values.

> But bottom line, I really thing the int base type is more than an
> implementation detail. I think it's natural for an _ordered_ set of
> names ;-)

I think I've addressed all your current concerns; I don't believe an
inherent correlation to integers is necessary at all.

It's no more necessary than saying that ["a", "b", "c"] requires that
there be some specific correlation between the values of that list and
the integers 0, 1, 2. If you *want* such a correlation, in some
particular case, use enumerate() to get it; but there's nothing about
the values themselves that requires that correspondence.

> I'll go look at PyPI now ;-)

Feedback appreciated :-)

-- 
 \     "Oh, I realize it's a penny here and a penny there, but look at |
  `\      me: I've worked myself up from nothing to a state of extreme |
_o__)                                       poverty."  -- Groucho Marx |
Ben Finney



More information about the Python-list mailing list