[Python-Dev] PEP 435 - ref impl disc 2

Glenn Linderman v+python at g.nevcal.com
Mon May 6 00:57:57 CEST 2013


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)...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130505/26566a1e/attachment.html>


More information about the Python-Dev mailing list