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

Ethan Furman ethan at stoneleaf.us
Sun Apr 24 12:42:10 EDT 2016


On 04/24/2016 09:10 AM, Chris Angelico wrote:
> On Mon, Apr 25, 2016 at 2:04 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> Unfortunately, the empty tuple tends to be a singleton, so there is no way
>> to tell that red and default are (supposed to be) the same and blue is
>> (supposed to be) different:
>>
>> --> a = b = ()
>> --> c = ()
>> --> a is b
>> True
>> --> a is c
>> True
>>
>> If you have an idea on how to make that work I am interested.
>
> 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:

class Numbers:
    def __init__(self, value=0):
       self.value = value
    def __call__(self, value=None):
       if value is None:
           value = self.value
       self.value = value + 1
       return value

a = Numbers()

class SomeNumbers(Enum):
    one = a()
    two = a()
    five = a(5)
    six = seis = a()

One extra character, and done.

--
~Ethan~



More information about the Python-list mailing list