[Python-Dev] Enumeration items: `type(EnumClass.item) is EnumClass` ?

Ethan Furman ethan at stoneleaf.us
Mon Apr 29 15:32:58 CEST 2013


[creating new thread]

On 04/29/2013 01:30 AM, Steven D'Aprano wrote:
> On Sun, Apr 28, 2013 at 11:50:16PM -0700, Ethan Furman wrote:
>
>> In other words, currently:
>>
>>    class Color(Enum):
>>        red = 1
>>        green = 2
>>        blue = 3
>>
>>    class MoreColor(Color):
>>        cyan = 4
>>        magenta = 5
>>        yellow = 6
>>        black = 7
>>
>>    MoreColor.red is Color.red  # True
>
> Correct.
>
>
>> But as soon as:
>>
>>    type(Color.red) is Color          # True
>>    type(MoreColor.red) is MoreColor  # True
>
> I don't believe this is correct. As I understand it, the proposal is the
> weaker guarantee:
>
>    isinstance(Color.red, Color) # True, possibly using __instancecheck__


Words from Guido:

On 04/23/2013 08:11 AM, Guido van Rossum wrote:
> I gotta say, I'm with Antoine here. It's pretty natural (also coming
> from other languages) to assume that the class used to define the
> enums is also the type of the enum values. Certainly this is how it
> works in Java and C++, and I would say it's the same in Pascal and
> probably most other languages.
>

On 04/25/2013 02:54 PM, Guido van Rossum wrote:
> I don't know what's going on, but it feels like we had this same
> discussion a week ago, and I still disagree. Disregarding, the C[i]
> notation, I feel quite strongly that in the following example:
>
> class Color(Enum):
>      red = 1
>      white = 2
>      blue = 3
>      orange = 4
>
> the values Color.red etc. should be instances of Color. This is how
> things work in all other languages that I am aware of that let you
> define enums.

On 04/25/2013 03:19 PM, Guido van Rossum wrote:
> I suppose you were going to propose to use isinstance() overloading,
> but I honestly think that Color.red.__class__ should be the same
> object as Color.

On 04/25/2013 03:37 PM, Guido van Rossum wrote:
> TBH I had a hard time getting over the fact that even though the class
> said "a = 1", C.a is not the integer 1. But I did get over it.
> Hopefully you can get over *this* weirdness.

[and from the summary thread]

On 04/28/2013 01:02 PM, Guido van Rossum wrote:
> On Sun, Apr 28, 2013 at 12:32 PM, Ethan Furman wrote:
>>
>>    - should enum items be of the type of the Enum class? (i.e. type(SPRING)
>> is Seasons)
>
> IMO Yes.


More information about the Python-Dev mailing list