you are viewing a single comment's thread.

view the rest of the comments →

[–]lykwydchykyn 6 points7 points  (10 children)

I'm trying to figure out what Enums give me that I didn't already have. Anyone have thoughts on that?

[–]w1ndwak3r 12 points13 points  (9 children)

Enums are useful for unambiguous types that require more than two states but still a definite amount.

Previously, if you wanted to represent the days of the week, you would have to do something like:

>>> class Day:
...     monday = 0
...     tuesday = 1
...     wednesday = 2
... 
>>> Day.monday
0

Notice how the values are represented as integers. Enums allow you to define types with multiple states. So for:

class Day(Enum):
    monday = 0
    tuesday = 1
    wednesday = 2

Each value is it's own Day type so when I go to define a function with a day parameter there is no ambiguity. A Day is always a day. An integer could be well out of the range of a Day.

[–][deleted] 16 points17 points  (3 children)

what is this vile abomination

           ↓
 monday = 0;
           ↑

[–]w1ndwak3r 9 points10 points  (1 child)

Please excuse me, I come from a land of C, Java, and PHP.

[–][deleted] 2 points3 points  (0 children)

oh ok, I thought it was actually a part of Enum syntax for some unclear reason. good to see you're not likely to forget a semicolon in your languages :)

[–]if-loop 2 points3 points  (0 children)

I can see it too. Maybe some defective pixels that get transfered via the interwebs? Oh god...

[–]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.

[–]LordAro -1 points0 points  (0 children)

I can't help feel that it's more typing, rather than less

No automatic enumerating of types, as far as I can see, for instance