Enumeration idioms: Values from different enumerations

Antoon Pardon apardon at forel.vub.ac.be
Fri Dec 16 05:57:51 EST 2005


Op 2005-12-16, Ben Sizer schreef <kylotan at gmail.com>:
> Ben Finney wrote:
>> The problem with "is the same value" as an explanation for '==' is
>> that it doesn't help in cases such as::
>>
>>     >>> ShirtSize = Enum('small', 'medium', 'large')
>>     >>> AppleSize = Enum('small', 'large')
>>
>> What should be the result of this comparison::
>>
>>     >>> ShirtSize.small == AppleSize.small
>>
>> Are they "the same value"? They're both "small" (and they both coerce
>> to the same string value, and in this case the same integer value).
>
> Is it possible to make it have the following sort of behaviour? :
>
>>>> ShirtSize.small == AppleSize.small
> True
>>>> ShirtSize.small is AppleSize.small
> False
>
> It works for comparing a boolean (True) vs. an integer (1), so it has
> some sort of precedent. (Especially if you make the tenuous assumption
> that True,False are language-supported 'enums' for 0 and 1.)

I'm against it. I don't like the following returning True:

  ShirtSize.small in [ShirtSize.Medium, AppleSize.small]


I also think it may cause problems with other comparisons.

Supose the following:

  col = Enum('red', 'green', 'blue')
  paint = Enum('violet' , 'blue', 'red')

Then we get the following situation: 

  col.red == paint.red and col.blue == paint.blue

but

  col.red < col.blue and paint.blue < paint.red

-- 
Antoon Pardon



More information about the Python-list mailing list