[Python-Dev] backported Enum

Guido van Rossum guido at python.org
Fri Jun 28 23:11:59 CEST 2013


On Fri, Jun 28, 2013 at 1:30 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 06/28/2013 01:07 PM, Jim J. Jewett wrote:
>> (On June 19, 2013) Barry Warsaw wrote about porting mailman from
>>> Switching from getitem syntax to call syntax for looking up an
>>> enum member by value, e.g.
>>>
>>>     -        return self._enum[value]
>>>     +        return self._enum(value)
>>>
>>> Interesting that these two were exactly opposite from flufl.enum.

>> Is there a reason why these were reversed?

> Originally the call syntax was used for both value and name lookup.  Various
> folks were unhappy with that arrangement, and since the use-case that I was
> thinking of at the time was getting enum members back from databases, etc.,
> I went with EnumClass(value); I still wanted to be able to use name lookups,
> so lobbied for getitem syntax for that.  Nobody noticed this was the
> opposite of flufl.enum.
>
> Oh, I say "I", and it is certainly my reasons, but I no longer remember if
> was me that initially proposed those specific ideas, and there were
> certainly many others that agreed.

I also have a strong preference for the new way of doing this. In
Python, type(arg) is usually a constructor or conversion/cast, and
that maps reasonably well on what we're doing here with enums --
taking a value and turning it into the corresponding enum (which still
shares some properties with that value, and more if it's an IntEnum).
Also, this is a no-op (technically, the identity function) if the arg
is already a member of that enum.

On the other hand, Color['red'] feels more like a lookup -- this is
not so much rhyming with the behavior of other classes (classes are
rarely mappings) but still the mnemonic tip "foo[bar] is a lookup"
should help in remembering the meaning.

I have no idea why it was the other way around in flufl.enum, but
admittedly neither of these rules are absolute, and it's possible that
flufl.enum just evolved that way without conscious decisions, or that
it came from taking a different perspective.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list