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

Alex Stewart foogod at gmail.com
Tue Mar 12 23:17:59 CET 2013


On Tue, Mar 12, 2013 at 2:31 PM, Alex Stewart <foogod at gmail.com> wrote:

> Regarding the issue of bitmask-enums, I do agree that they are common
> enough in various APIs that it is important that we be able to support them
> easily.  *However,* I have yet to see how or why they are actually
> different than int-enums in any practical way.  I don't see why we need to
> treat them as a different category at all and I see no value in doing so.
>  They're all just* *int-enums.  Problem solved.
>

It just occurred to me that as I was skimming some of the previous
discussion and I was also coming from the perspective of my own
implementation, which has not yet been shared with the rest of the group,
this response may have been overly terse and not really communicated what I
meant very well.

For clarification:  I believe that what we're fundamentally talking about
here is the question of "compounding" enum values into what are effectively
multi-enum sets (they are not really the same thing anymore as a single
enum value, because they do not have a one-to-one correspondence with any
one enum value).  With ints, this compounding operation is typically a
"binary or" operation.  This is what we call "bitmasks", but really they're
just a particular way of compounding ints together.

In my opinion, the concept of "compound enum" is a larger abstract concept
that could, and probably should, apply to any type of enum, not just ints.
 In that context, bitmasks are really nothing special, they are just the
compound form of int-enums:

class Color (Enum):
    RED, GREEN, BLUE = __ * 3

class WxStyle (IntEnum):
    RESIZE_BORDER = 64
    MAXIMIZE_BOX = 512

>>> x = Color.RED | Color.BLUE
>>> repr(x)
'Color.RED | Color.BLUE'
>>> Color.RED in x
True
>>> Color.GREEN in x
False
>>> type(x)
<class 'enum.CompoundEnum'>
>>> int(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'CompoundEnum'

>>> x = WxStyle.MAXIMIZE_BOX | WxStyle.RESIZE_BORDER
>>> repr(x)
WxStyle.MAXIMIZE_BOX | WxStyle.RESIZE_BORDER
>>> WxStyle.MAXIMIZE_BOX in x
True
>>> type(x)
<class 'enum.CompoundIntEnum'>
>>> int(x)
576

(This may become a bit clearer when you guys can all see the implementation
I've been coding up)

--Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130312/05fbdafe/attachment.html>


More information about the Python-ideas mailing list