6 ms·
I don't think that enums should have values (i.e. numbers). Nearly every language does it that way but isn't that just because C originally implemented enums w
by adrianmsmith 13y ago
I don't think that enums should have values (i.e. numbers).
Nearly every language does it that way but isn't that just because C originally implemented enums with numbers? Modern languages are much higher level.
Java enums don't have any numeric equivalent value, they are just objects, with textual names if you must convert them into something more primative.
- Nursie 13y agoEfficiency/speed? The user doesn't necessarily need to have access to the raw numbers I guess, but when it comes down to comparisons/branching etc, numbers are pretty much always going to be fastest and simplest. And if they are going to be based on numbers underneath, why not let the user/coder/whatever have access to them?
- comex 13y agoAs an option, it can be handy in some cases (less in Python than C, but still somewhat); requiring that you write them out for all enums, however, is somewhat annoying. C itself gets this right: you can write '= 1' to choose a value or leave it out to assign one automatically, and in either case you can convert to an integer (actually, they're integers in the first place, but that's C).
- mikeash 13y agoEverything is numbers underneath, so you could ask the same question about all data types. For example, if pointers are going to be based on numbers underneath, why not let the programmer have access to them? Lots of good reasons, actually, which is why most languages don't provide that access. Many of those reasons apply to enumerations too.
- Nursie 13y agoWell, you've outed me as a C programmer there I guess :) I don't really subscribe to the idea that languages should force people onto rails. I love higher level languages for their rapid development of course, and the high level constructs. But there's something about being able to get your hands dirty, right in the guts of a problem...
- eliben 13y agoEnum values don't have to be ints, they can have any value you want. The explicit values are there because Python's "explicit is better than implicit" zen, and the functional API gives you a way to create enums without assigning explicit values if you really want to.
- famousactress 13y agoI always thought the fact that Java enums don't have numeric values ended up being a nice idea, but a mistake.. specifically because .ordinal was still exposed, and ends up getting used by people who want to serialize the enum as something other than a string... .ordinal of course is affected by the order the enum is defined in, which is sketchy. My memory could be a little faded on this though, I haven't coded in Java in a few years..
- iso8859-1 13y agoif you use "None" with this PEP, you'll get the same effect.
- brown9-2 13y agoJava enums have int ordinal values: http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#ordinal() http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html... I think what you mean though is that in 99.9% of cases a user of a Java enum never needs to (or shouldn't) refer to the ordinal value.
- mattmanser 13y agoIt can actually be pretty useful to be able to add them up and then use them in or statements. Examples from C#: http://msdn.microsoft.com/en-us/library/vstudio/cc138362.aspx http://msdn.microsoft.com/en-us/library/vstudio/cc138362.asp... It's also used to be sort of useful for serialization or storing them in a DB as they'd take up less space than a string, but these days that's not so important and I'd actually caution against it. Basically you don't need it often, but when you do it's extremely useful.
- sp332 13y agoDoing binary arithmetic on enums can be very useful! First | Second = Both.
- InclinedPlane 13y agoThat's what enum flags in c# are for, it's a very slick feature.
- kstrauser 13y agoOne approach would be to set values like "red = object()". I use that often to create guaranteed-unique sentinel values.
- ekimekim 13y agoMy approach was this: class UniqueValue: pass