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

Ethan Furman ethan at stoneleaf.us
Sun Apr 24 14:45:10 EDT 2016


On 04/24/2016 11:27 AM, Chris Angelico wrote:
> On Mon, Apr 25, 2016 at 4:12 AM, Ethan Furman wrote:

>> Values can be anything.  The names are immutable and hashable.
>
> I know they *can* be, because I looked in the docs; but does it make
> sense to a human? Sure, we can legally do this:

Well, not me.  ;)

>--> class Color(Enum):
> ...     red = 1
> ...     green = 2
> ...     blue = 3
> ...     break_me = [0xA0, 0xF0, 0xC0]
> ...
>--> Color([0xA0, 0xF0, 0xC0])
> <Color.break_me: [160, 240, 192]>
>--> Color([0xA0, 0xF0, 0xC0]).value.append(1)
>--> Color([0xA0, 0xF0, 0xC0]).value.append(1)

If you are looking up by value, you have to use the current value. 
Looks like pebkac error to me.  ;)


> At some point, we're moving beyond the concept of "enumeration" and
> settling on "types.SimpleNamespace".

Sure.  But like most things in Python I'm not going to enforce it.  And 
if somebody somewhere has a really cool use-case for it, more power to 'em.

--
~Ethan~



More information about the Python-list mailing list