Comparing Python enums to Java, was: How much sanity checking is required for function inputs?

Chris Angelico rosuav at gmail.com
Sun Apr 24 14:01:40 EDT 2016


On Mon, Apr 25, 2016 at 3:54 AM, BartC <bc at freeuk.com> wrote:
> On 24/04/2016 17:47, Chris Angelico wrote:
>>
>> On Mon, Apr 25, 2016 at 2:42 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>>>>
>>>> Easy: allow an empty list to have the same meaning as an empty tuple.
>>>> Every time you have [] in your source code, you're guaranteed to get a
>>>> new (unique) empty list, and then multiple assignment will work.
>>>
>>>
>>>
>>> *sigh*
>>>
>>> Where were you three years ago?  ;)
>>>
>>> Actually, thinking about it a bit more, if we did that then one could not
>>> use an empty list as an enum value.  Why would one want to?  No idea, but
>>> to
>>> make it nearly impossible I'd want a much better reason than a minor
>>> inconvenience:
>>
>>
>> I would normally expect enumerated values to be immutable and
>> hashable,
>
>
> And, perhaps, to be actual enumerations. (So that in the set (a,b,c,d), you
> don't know nor care about the underlying values, except that they are
> distinct.)

Not necessarily; often, the Python enumeration has to sync up with
someone else's, possibly in C. It might not matter that BUTTON_OK is 1
and BUTTON_CANCEL is 2, but you have to make sure that everyone agrees
on those meanings. So when you build the Python module, it's mandatory
that those values be exactly what they are documented as.

ChrisA



More information about the Python-list mailing list