On 12 February 2013 07:09, Tim Delaney <timothy.c.delaney@gmail.com> wrote:

Supporting this functionality was actually very simple. However, I am wondering though how useful this is without being able to specify additional parameters for the enums. I've often used enums with quite complex behaviour (e.g. in java) - the enum part is purely to have a unique value assigned to each instance of the class and not repeating myself for no good reason. I couldn't quite do the same with this as it currently is - whilst I can have whatever behaviour I want, there's nothing to key it off except the name and value which probably isn't enough. I've been trying to think of a syntax which would work to pass additional parameters and I can't think of anything cleaner than having a specialised class to pass the additional parameters - but I need to somehow be able to specify the optional integer value of the enum. Maybe have the first parameter be None?

OK - I've implemented this. EnumValue has grown 'args' and 'kwargs' attributes.

Enums can be constructed like:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import Enum, EnumParams, _
>>>
>>> class MyEnum(Enum):
...     A, B
...     C = EnumParams(None, 'i', 'j', k='k', l='l')
...     D = EnumParams(10, 'm', n='n')
...     E
...     F = EnumParams(None, 'o')
...     G = 20
...     H, I
...     J = EnumParams(None, p='p')
...
>>> print(repr(MyEnum))
<enum __main__.MyEnum {<EnumValue 'A': 0>, <EnumValue 'B': 1>, <EnumValue 'C': 2>, <EnumValue 'D': 10>, <EnumValue 'E': 11>, <EnumValue 'F': 12>, <EnumValue 'G': 20>, <EnumValue 'H': 21>, <EnumValue 'I': 22>, <EnumValue 'J': 23>}>

_ is aliased to EnumParams so you can do:

>>> class MyEnum(Enum):...     A, B
...     C = _(None, 'i', 'j', k='k', l='l')
...     D = _(10, 'm', n='n')
...     E
...     F = _(None, 'o')
...     G = 20
...     H, I
...     J = _(None, p='p')
...
>>> print(repr(MyEnum))
<enum __main__.MyEnum {<EnumValue 'A': 0>, <EnumValue 'B': 1>, <EnumValue 'C': 2>, <EnumValue 'D': 10>, <EnumValue 'E': 11>, <EnumValue 'F': 12>, <EnumValue 'G': 20>, <EnumValue 'H': 21>, <EnumValue 'I': 22>, <EnumValue 'J': 23>}>

which looks a bit cleaner to me, but might be a little confusing. Thoughts?

Tim Delaney