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

Glenn Linderman v+python at g.nevcal.com
Sun May 5 10:01:12 CEST 2013


On 5/5/2013 12:21 AM, Ethan Furman wrote:
> On 05/04/2013 11:31 PM, Glenn Linderman wrote:
>>
>> x = NamedInt('the-x', 1 )
>> y = NamedInt('the-y', 2 )
>> # demonstrate that NamedInt propagates the names into an expression 
>> syntax
>> print( repr( x ), repr( y ), repr( x+y ))
>>
>> from ref435 import Enum
>>
>> # requires redundant names, but loses names in the expression
>> class NEI( NamedInt, Enum ):
>>      x = NamedInt('the-x', 1 )
>>      y = NamedInt('the-y', 2 )
>>
>> print( repr( NEI( 1 )), repr( NEI( 2 )), repr( NEI(1) + NEI(2)))
>
> Well, my first question would be why are you using named anything in 
> an enumeration, where it's going to get another name?

:)  It is a stepping stone, but consider it a stupid test case for now.

> But setting that aside, if you
>
> --> print(NEI.x.__name__)
> 'x'
>
> not 'the-x'.
>
> Now let's look for the clues:
>
> class Enum...
>     ...
>     @StealthProperty
>     def name(self):
>         return self._name
>
> class NamedInt...
>     ...
>     def __name__(self):
>         return self._name  # look familiar?
>
>
> When NamedInt goes looking for _name, it finds the one on `x`, not the 
> one on `x.value`.

Indeed.

But that isn't the problem of biggest concern.  I changed NamedInt to 
use _intname instead of _name, and it didn't cure the bigger problem.

The bigger problem is that the arithmetic on enumeration items, which 
seems like it should be inherited from NamedInt (and seems to be, 
because the third value from each print is a NamedInt), doesn't pick up 
"x" or "y", nor does it pick up "the-x" or "the-y", but rather, it 
somehow picks up the str of the value.

The third item from each print should be the same in all print 
statements, but the first, that deals with the NamedInt directly, works, 
and the others, that are wrapped in Enum, do not.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130505/2578beca/attachment.html>


More information about the Python-Dev mailing list