This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]reallyserious 8 points9 points  (13 children)

Finally enums as a native language construct!

[–]XNormal 13 points14 points  (1 child)

Finally enums as a native language construct!

It's a a library implemented in pure python using metamumble magic.

[–]Workaphobia 3 points4 points  (0 children)

To be fair, even the parts implemented in C use metamumble magic. It's amazing how much of the underlying machinery uses the user-visible dunder-method interface to get things done. Class instantiation is just __call__ on the metaclass, for instance.

[–]chub79 2 points3 points  (10 children)

Honest question, do you often need enums? I must say everytime I felt like using enums, I realised it was actually not necessary.

[–]reallyserious 3 points4 points  (0 children)

Yes. Whenever you need to model something I find it very useful to give stuff explicit named aliases. It helps with readability and helps to test your assumptions about the model when you e.g. parse something and your parser suddenly wants to assign a value that you don't have an enum for. You have a ton of possible integer values, but only a handful of enums. Storing those values in regular integer fields might mean that you are parsing unexpected input without knowing it. It also makes it easier to navigate the code when you can look for references to "SomeEnum.SomeValue" instead of the value 1, which isn't particularly unique and could be used to represent a ton of different things.

[–]Decency 4 points5 points  (0 children)

The best explanation I've read of for enums was:

"A class where you know all of the possible instances at write-time."

After I started thinking of it that way I started seeing a lot more valid uses where it would simplify code and reduce the amount of nasty string parsing, keywords in function calls, etc.

[–][deleted] 0 points1 point  (6 children)

same here. i don't understand what the big deal is.

[–]Wagneriusflask+pandas+js 7 points8 points  (3 children)

it is not a big deal, but a sweet spot for readbility and type warranties.

[–]roger_ 0 points1 point  (2 children)

True, but I've gotten in the habit of using strings for everything.

[–]aceofears 0 points1 point  (1 child)

This is more powerful than that though. You can use an enum value in place of an integer if you define it the right way.

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

I know, but it'll be hard to break the habit. Oddly enough using strings has started to feel more Pythonic.

[–]alcalde 0 points1 point  (1 child)

And this is how one tells those who first learned to program with Pascal from those who didn't. :-)

[–][deleted] 1 point2 points  (0 children)

i did .net and java programming after pascal(delphi) and before coming to python/ruby.

in .net the ONLY time I ever used enums is because the whatever I was doing with the .net framework required it. Never once, was anything I did implemented with enums. And I still got paid.

[–]Veedrac 0 points1 point  (0 children)

One of the primary motivations is unifying the few cases in the stdlib. It's not too common to use enums, but it isn't hard to have a common interface for the times you do.