
Brian Skinn wrote:
Steve, It looks like what you're trying to achieve is an Enum with more than a singleton .value exposed on each sub-item. I was able to achieve something that looks like your example using an Enum mixin with a custom class:
from enum import Enum class Labeler: @property def label(self): return self.name.capitalize() class Size(Labeler, Enum): SMALL = "S" MEDIUM = "M" LARGE = "L" Size.LARGE.label 'Large' Size.LARGE.value 'L' More custom values could be added by expanding Labeler. FWIW, I also got a mixin with namedtuple to work: from collections import namedtuple as nt Key = nt('Key', ('label', 'value')) class Foo(Key, Enum) Bar = Key(label='Cat', value=5) Baz = Key(label='Dog', value=10) Foo.Bar.label 'Cat' These both were with Python 3.7. -Brian
Yes. I thought of that approach, and it does work, but it still forces developers to do unnecessary extra work and potentially have to compromise what they're actually trying to do. One thing this will specifically not support is having the name and the value each be optionally based on the name. If providing an explicit value for the label but not the name (maybe like `FOO = (auto(), 'Fooo')`, then since the value is a `tuple` (in spite of containing an `auto`) `_generate_next_value_` will not be invoked, so the next time we can get the name is during `_init_ which is too late to set `_value_`. Maybe a simpler solution than what I proposed would be if we could wait until after `__init__` is called before locking down `_value_`, but I assume that would already be allowed if there weren't some problem with trying to allow it.