PEP 354: Enumerations in Python

Stefan Rank stefan.rank at ofai.at
Tue Feb 28 04:19:29 EST 2006


on 28.02.2006 07:50 Carl Banks said the following:
> Ben Finney wrote:
>> This PEP specifies an enumeration data type for Python.
> 
[snip]
> 
> Here's why I think it's not too useful to begin with: the benefits of
> the enum you describe here are pretty weak.

I need to disagree heavily here :)

+1 from me for the general idea of a builtin enum.

(and for 'real world' use cases: I use this type of enum, the PEP one, 
in my code regularly)

> It's a pretty weak case to have a dedicated builtin to prevent
> duplicates in something that changes maybe once a month, as enums tend
> to change rather slowly.  (At least, that's the way enums in other
> languages are used, and the design you present here seems to suggest
> you intend to use them that way as well.)  And frankly, a unit test or
> assertion could check this.
[snip]

I don't understand what you mean by 'change rather slowly'?
The dominant use case for an explicit enum is to make it clear for the 
code user that the values are a separate type, and prevent errors 
occurring because the abused underlying type shows through (be it 
strings or integers) or at least give informative warnings for them. If 
you want more than that something, like a dict, will probably be better.

recent examples from this list:

2006-01-03: http://www.nabble.com/Re%3A-Regex-anomaly-p2179421.html
2006-02-20: 
http://www.nabble.com/Re%3A-Regular-expression-gone-mad-p3029028.html

> The nonsensical comparisions should throw value errors.

That's a valid point.

I hope someone knowledgeable will go through the standard library and 
check each flag-like thing (which is not directly dependent on an 
underlying c-library idiom) against the proposed enum type.

One thing that is probably missing to allow this, is a enum-set-creation 
with the | operator::

   Weekdays = enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
   daysihate = Weekdays.mon | Weekdays.thu

(and this discussion needs to move to python-dev ?)

As for the metaclass versions: For myself, the above version feels more 
natural and straightforward (in the same way as the PEP author describes 
it), though I understand the subclassing ideas.

But are there use cases for subclassing, that aren't better served with 
a new enum or something homegrown?
Can C++/Pascal/Java enums be subclassed?

cheers,
stefan




More information about the Python-list mailing list