On 5/5/2013 6:07 AM, Ethan Furman wrote:
class NEI( NamedInt, Enum ):
    x = NamedInt('the-x', 1 )
    y = NamedInt('the-y', 2 )
    @property
    def __name__(self):
        return self.value.__name__

This cured it, thank you. But I really still don't understand why the numbers showed up as names... seems to me that either the name of the enumeration member, or the name of the NamedInt should have showed up, without this property definition.

The PEP is the only documentation I had, other than the reference implementation, but I can't say I fully understand the reference implementation, not having dealt with metaclass much. Hopefully the documentation will explain all the incantations necessary to make things work in an expected manner. I guess I don't understand why Enum can't wrap the __str__ and __repr__ of the type of the mixed class, instead of replacing it, and then forcing overrides in subclasses.

But empirically, it didn't seem to be __str__ and __repr__ that caused the visible problem, it was __name__. So you asked why would I want to put a named object as the value of something else with a name... and that is a fair question... really I don't... but I see one of the beneficial uses of Enum being collecting flags values together, and constructing a flag that is useful for debugging (has a name or expression telling what the values are). So while the PEP thinks IntEnum is an odd case, I think it is important. And since IntEnum loses its name when included in an expression, I was trying to marry it to NamedInt to fill the gap.

So you asked why would I want to put a named object as the value of something else with a name... maybe Enum should make provision for that... if the primary type ( Int for IntEnum, NamedInt for NamedIntEnum) happens to have a __name__ property, maybe the name of enumeration members should be passed to the constructor for the members... in other words,

class NIE( NamedInt, Enum ):
        x = 1
        y = 2

could construct enumeration members x and y whose values are NamedInt('x', 1) and
NamedInt('y', 2)...
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to