[Python-ideas] str-type enumerations

Ethan Furman ethan at stoneleaf.us
Thu Apr 4 21:52:40 CEST 2013


On 04/04/2013 10:22 AM, Eli Bendersky wrote:
>
> On Thu, Apr 4, 2013 at 9:23 AM, Ethan Furman wrote:
>
>     In the example above 'Mn$' is the field, and '(1,6)' are the first six characters in the field.  So the custom
>     mapping has to parse the key passed to in in order to return the proper value; it looks something like this:
>
>          def __getitem__(self, key):
>              real_key = key[:3]
>              field = self.dict[real_key]
>              first, length = key[3:][1:-1].split(',')
>              first = first - 1
>              last = first + length
>              data = field[first:last]
>              return data
>
>     If key is a str-based enum, will this work?
>
> No. Enum allows having string values, that's it. The object itself is a EnumValue, though, it's not "isinstance(str)".
>
> That's a strange use case, though :-)

Yeah, well, Python is a glue language, and sometimes glue is messy.  ;)

--
~Ethan~



More information about the Python-ideas mailing list