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

Guido van Rossum guido at python.org
Mon Apr 29 17:39:41 CEST 2013


Indeed, the "type(Color.red) is Color" claim was meant for the
situation where red is defined directly in Color, and I used type()
instead of isinstance() because Barry was proposing to overload
isinstance() to make this true without equating the classes. But for
the subclass case, I want MoreColor.red is Color.red and
isinstance(MoreColor.red, Color), but not isinstance(Color.red,
MoreColor). If you can't live with that, don't subclass enums.

On Mon, Apr 29, 2013 at 6:32 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> [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.
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/guido%40python.org



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list