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

Ethan Furman ethan at stoneleaf.us
Sun May 5 09:21:22 CEST 2013


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?
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`.
--
~Ethan~


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /