[Python-ideas] Enums

Nick Coghlan ncoghlan at gmail.com
Thu Jul 28 02:30:42 CEST 2011


On Thu, Jul 28, 2011 at 9:00 AM, Barry Warsaw <barry at python.org> wrote:
> On Jul 28, 2011, at 08:24 AM, Nick Coghlan wrote:
>
>>For compatibility reasons, though, I believe it is worth following the
>>precedent set by bool: if integer constants are to be replaced by
>>named values, the replacement needs to support everything the previous
>>value did. The easiest way to achieve that is by inheriting directly
>>from the previous value's type.
>
> I mentioned in a separate follow up that I'd be open to that, if someone wants
> to submit a patch.

I did see that, but I don't see a straighforward way to do it without
locking the enum implementation into being *specifically* for
integers.

>>1. I find the "enum" concept too limiting. NamedValue (as a mixin
>>class) is a better building block since it separates the naming aspect
>>from the "group of values" aspect implied by enum.
>
> I think that's all fine, but it's getting far from *my* simple concept of
> enumerated values.  Go for it though. :)

How is something that does *less* more complicated?

This recipe is pretty much the full extent of the proposal (perhaps
with the automatic type generation I mention in the discussion
section):
http://code.activestate.com/recipes/577810-named-values/

>>But the grouping behaviour? I really don't see a lot of value in that
>>- just boilerplate. We don't write bool.True and bool.False, we write
>>True and False.
>
> That's only because True and False are bound in a particular module namespace.
> There's no reason why specific EnumValues couldn't also be similarly bound.

But the *necessity* of grouping creates a lot of boilerplate and
prevents TOOWTDI.

The obvious way to do constants in current Python is as module level
constants. The obvious way to do enums, on the other hand, is to have
a grouping class that holds the names. Translating between the two
requires a boilerplate loop over the enum values to add references
into the module namespace.

Whereas, with the named value approach, all you *have* to do is change
"named_constant = x" to "named_constant =
named_value('named_constant', x)".

The various enum libraries could then become convenience APIs to make
it easier to create groups of named values.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list