[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library (original) (raw)

Barry Warsaw barry at python.org
Thu Apr 25 23:39:45 CEST 2013


On Apr 25, 2013, at 02:25 PM, Eli Bendersky wrote:

I think it's important to stress what this syntax is actually going to be used for. No one (I hope) is actually going to write Animals(1) or Animals[1]. They will write Animals.ant - this is what enums are for in the first place! The way I see it, this syntax is for enabling *programmatic access* - if you pull the value from a DB and want to convert it to an actual enum value, etc. So do we really need to have two syntaxes for this?

Excellent point, and no, we don't :).

The call syntax already has other uses, and it's weird because:

Enum(....) -> Creates new enums Animals(....) --> accesses values ?! This is contradictory Animals[...] to serve as a by-value lookup makes sense, though.

I think so too. :)

Note that I discovered that the same two-value call syntax on Enum can be used on the derived classes. It's exactly the same as using subclassing syntax to extend an existing enum. E.g.

>>> class A(Enum):
...   a = 1
...   b = 2
...   c = 3
... 
>>> class B(A):
...   d = 4
...   e = 5
... 
>>> B.a is A.a
True
>>> X = Enum('X', 'a b c')
>>> Y = X('Y', (('d', 4), ('e', 5)))
>>> Y.a is X.a
True

That's a nice symmetry.

-Barry -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-dev/attachments/20130425/16592f65/attachment.pgp>



More information about the Python-Dev mailing list