[Python-ideas] PEP XXX - Competitor with PEP 435: Adding an enum type to the Python standard library

Ethan Furman ethan at stoneleaf.us
Tue Mar 12 21:40:03 CET 2013


On 03/12/2013 01:07 PM, Andrew Barnert wrote:
> 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)

With a BitMask this could be:

     window = wx.Frame(None, style=wx.Style('MAXIMIZE_BOX|RESIZE_BORDER|SYSTEM_MENU|CAPTION|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)
>      print(background < edge)
>
> The benefits of NamedInt are that this doesn't print "1 4" and then "True".

Hopefully you meant BitMask, as a NamedInt would result in True (unless either color or side were not NamedInts).

While a BitMask is not directly an int, Barry showed how it turns into an int when passed to a C library.


> Now:
>
>
>      style = wx.styles.MAXIMIZE_BOX | wx.styles.RESIZE_BORDER
>      print(style)
>      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.

How valuable is a name?  And a doc string?  Currently I'm working with OpenERP code, which uses constants in a lot of 
key places.  I would love to know what 5 stands for, and 6.  I knew 6 once, kind of, for about ten minutes while I 
worked with it, and now I've forgotten.

--
~Ethan~



More information about the Python-ideas mailing list