[Python-ideas] PEP XXX - Competitor with PEP 435: Adding an enum type to the Python standard library
abarnert at yahoo.com
Tue Mar 12 21:07:05 CET 2013
From: Eli Bendersky <eliben at gmail.com>
Sent: Tuesday, March 12, 2013 10:28 AM
>On Tue, Mar 12, 2013 at 9:59 AM, Andrew Barnert <abarnert at yahoo.com> wrote:
>On Mar 12, 2013, at 8:36, Eli Bendersky <eliben at gmail.com> wrote:
>>> It is actually better, because it emphasizes that NamedInt is just that, not a kind of Enum. There's just one enum. Moreover, I'm not sure why strings need to be named (they name themselves just fine). And moreover+, Bitmask IMHO is completely unnecessary in Python.
>>It's necessary everywhere we interface with C APIs and binary formats that use them. Even the stdlib is full of candidates--the flags in os, stat, etc. are all bitmasks.
>I think that viewing the Python programmer community at large, very few actually interact with C APIs that have bitmasked flags.
I don't see why this even needs to be established. We have cases like, e.g., mmap.mmap all over the stdlib that take bitmasked flags. Are you arguing that these functions are too uncommon to belong in the stdlib or need to be redesigned?
And, even if you don't touch any of those parts of the stdlib, there are many outside libraries that follow its example. From the "first steps" of the wx tutorial:
window = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
This isn't some design quirk of wx; this is how nearly every GUI framework except tkinter works. And image processing, audio processing, platform glue like win32api and PyObjC, and countless other application areas.
>Moreover, a NamedInt can fit the bill without needing a specific bitmask flag.
>If you have "names" for your flag constituents you can just join them with '|' as in C. This is similar to what's currently being done in modules like os and stat, but provides conveniently printable names for the magic numbers. The benefits of a specific bitmasking class in the stdlib are imho very marginal.
The benefits of a bitmasking enum class are exactly the same as the benefits of an ordered enum class. Compare:
background, edge = color.RED, side.BOTTOM
print(background < edge)
The benefits of NamedInt are that this doesn't print "1 4" and then "True".
style = wx.styles.MAXIMIZE_BOX | wx.styles.RESIZE_BORDER
print(style & wx.keycodes.TAB)
The benefits of a NamedInt are that this doesn't print "2098176" and then "True".
If we don't have these benefits, there is no reason to add NamedInt in the first place, because it's nothing more than an alternate way to construct integer constants.
Again, you could argue that making NamedInt work for the ordered case is trivial, making it work for the bitmask case is complicated, and therefore it's not worth doing even though it would be useful. But it clearly would be useful, for exactly the same reasons as in the ordered case.
More information about the Python-ideas