[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Ethan Furman ethan at stoneleaf.us
Thu Apr 25 23:30:11 CEST 2013


On 04/25/2013 02:17 PM, Barry Warsaw wrote:
> On Apr 25, 2013, at 01:18 PM, Ethan Furman wrote:
>
>>> Ignore the single argument call syntax for Enums please.  As Eli pointed
>>> out, you have getitem syntax for this and the single argument call syntax
>>> is deprecated.  It will be removed in a future version of flufl.enum and
>>> need not appear in stdlib enum.  TOOWTDI.
>>
>> For me, the getitem syntax on a class seems odd and the call syntax is
>> TOOWTDI.
>
> Not if you think of it as a lookup operation instead of an instantiation
> operation.  It really is the former because neither syntax creates new enum
> item objects, it just returns an already existing one.

True, but I don't.  ;)

I think the closest comparable thing in Python is the boolean class; while True and False are not attributes of bool, 
they are the only two instances, and invoking bool is going to return one of the existing bool instances (which is to 
say, True or False).  It's basically a conversion from whatever to bool.

--> bool('something')  # returns True

--> bool(None)  # returns False

Similarly, an Enum converts a string or a number to it's comparable enumerator (right word, Stephen?)

--> class Animal(Enum):
...     ant = 1
...     bee = 2
...     fly = 3

--> Animal(2)  # should return Animal.bee

--> Animal('ant')  # should return Animal.ant

It seems to me that storing the instances on the class as attributes is mostly for convenience; we could just as easily 
not, and change the repr of enumerators to '<Animal> ant [int=1]'.

--
~Ethan~


More information about the Python-Dev mailing list