[Python-ideas] str-type enumerations

Eli Bendersky eliben at gmail.com
Thu Apr 4 05:23:48 CEST 2013


On Tue, Apr 2, 2013 at 12:25 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> I'm not trying to beat this dead horse (really!) but back when the
> enumeration discussions were going fast and furious one of the things asked
> for was enumerations of type str, to which others replied that strings
> named themselves.
>
> Well, I now have a good case where I don't want to use the string that
> names itself: 'Mn$(1,6)'  I would match rather write item_code!  :)
>

Hi Ethan,

The latest incarnation of flufl.enum that went through the round of
discussions during PyCon allows string values in enumerations, if you want
them:

>>> from flufl.enum import Enum
>>> class WeirdNames(Enum):
...   item_code = 'Mn$(1,6)'
...   other_code = '#$%#$^'
...
>>> WeirdNames.item_code
<EnumValue: WeirdNames.item_code [value=Mn$(1,6)]>
>>> i = WeirdNames.item_code
>>> i
<EnumValue: WeirdNames.item_code [value=Mn$(1,6)]>
>>> i.value
'Mn$(1,6)'
>>> print(i)
WeirdNames.item_code
>>>

I haven't updated PEP 435 to reflect this yet, hope to do so in the next
day or two.

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130403/032a9e16/attachment.html>


More information about the Python-ideas mailing list