[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library (original) (raw)
Barry Warsaw barry at python.org
Mon Apr 22 02:31:12 CEST 2013
- Previous message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Next message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Apr 22, 2013, at 09:31 AM, Tim Delaney wrote:
I'm fine with iteration order being by sorted name by default, so long as it's easily overrideable by enum subclasses or metaclasses e.g. an IntEnum should probably iterate in value order.
It does. :)
For definition order, a 3.x-only metaclass could be provided:
class Days(enum.Enum, metaclass=enum.DefinitionOrder): Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4 Friday = 5 Saturday = 6 Sunday = 7
Yep, that's how it works. From flufl.enum:
class IntEnumMetaclass(EnumMetaclass): # Define an iteration over the integer values instead of the attribute # names. def iter(cls): for key in sorted(cls._enums): yield getattr(cls, cls._enums[key])
IntEnum = IntEnumMetaclass(str('IntEnum'), (Enum,), { 'doc': 'A specialized enumeration with values that are also integers.', 'value_factory': IntEnumValue, })
-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/20130421/625346fa/attachment.pgp>
- Previous message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Next message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]