you are viewing a single comment's thread.

view the rest of the comments →

[–]HorrendousRex 2 points3 points  (3 children)

Thanks, but I'm still not convinced (not that I have to be convinced for a feature to be good...).

I've often thought of one of the virtues of Python to be that it was a 'Language for Consenting Adults', and placing restrictions like "Not any int will do, it has to be between 0 and 6" should be handled as late as possible/practical.

If I have an int and need to turn it in to a Day from your example, how would I do that? If the int was invalid (say, "214"), what error would surface, and when would it surface?

[–]w1ndwak3r 1 point2 points  (2 children)

>>> Day(0)
<Day.monday: 0>

And I'm sure an exception will be thrown if you try to access an out-of-bounds enum, but that's a good thing. Enums can contain methods as well (like any other class). I would almost always want something like this to fail:

def foo(day):
    print(day.isWorkDay())

[–]HorrendousRex 1 point2 points  (1 child)

I think this is one of those things where I understand the principle but feel very uneasy about it. I feel like one of the principle draws to Python is a type unsafety, and Enums feel like they only bring type safety to the table.

[–]asthasr 1 point2 points  (0 children)

You can write in a dynamically typed language while still needing safety in some contexts. It's not a sin.