10 ms·
The syntax strikes me as somewhat strange. It requires writing out enum values (and thus avoiding duplicates or, for a cleaner look, just keeping them sorted),
by comex 14y ago
The syntax strikes me as somewhat strange. It requires writing out enum values (and thus avoiding duplicates or, for a cleaner look, just keeping them sorted), necessitating unnecessary effort when new values are added in the middle, and misleadingly implies that the enum values are merely class-level integer constants. I would have preferred a more explicit API that used a list of strings for the enumerators.
- maxerickson 14y agoSimilar to this? http://www.python.org/dev/peps/pep-0435/#functional-api http://www.python.org/dev/peps/pep-0435/#functional-api Having to throw in a " ".join(...) isn't that bad.
- ulope 14y agoNo need for join(). > It can be a whitespace-separated string of names, a sequence of names, a sequence of 2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to values.
- maxerickson 14y agoEven better. The many levels of skimming on display.
- oellegaard 14y agoI completely agree, this disgusts me: >>> Animal = Enum('Animal', 'ant bee cat dog') >>> Animal.ant <Animal.ant: 1> That being said, I welcome enums - I made classes to basically represent an enum a bunch of times.
- eliben 14y agoWhat disgusts you about it, in particular?
- ekimekim 14y agoThe fact that it allows whitespace-seperated words. It's just wrong. It's weird, unnecessary, it just shouldn't be there. Personally, I would've preferred: >>> Enum('Example', 'foo', 'bar', 'baz') (ie. Enum(name, *values))
- eliben 14y agoThat syntax is a bit unusual, but it has the clear advantage of being far less verbose. Consider this: * The functional API is mostly for short code snippets and experiments in the shell. For real programs, the standard class syntax is preferred and recommended. * namedtuple already uses the same functional API allowing space-separated members, and it's a popular tool in the stdlib. Yes, the first time you run into it it feels a bit odd, but then you get used to it and it's not a big deal.
- ulope 14y agoThe second parameter can also be a list/tuple of strings. e.g.: >>> Animal = Enum('Animal', ('lion', 'tiger', 'liger', 'tigon'))
- Tobu 14y agoStrange that the auto-assigned values start at one.
- w0utert 14y agoNot that strange, actually. Reserving zero allows using it as an 'undefined' enumeration value that plays well with basic Boolean tests, e.g. 'if not animal: ...' and such. Now all I want in Python is a real switch statement...
- Tobu 14y agoIf this was a strongly-typed language I wouldn't want enums to be nullable, but maybe people would use that in Python. Incidentally, this makes me wonder if one could implement something similar for flags (powers of two values, overloading | and &). Though sets of enums might be sufficient.
- ubernostrum 14y agoPython is a strongly-typed language. Perhaps you're thinking of static versus dynamic typing?
- Tobu 14y agoThe only languages I'd consider strongly typed are static typed. Python functions are always 'a -> 'b (any to any), the weakest possible type for a function. Same for variables (as opposed to values), they are all 'a; same for list membership or attribute membership. There is no place in the language with room for a nullable / nonnullable distinction.
- andreasvc 14y agoIn a dynamic language it's the values that are typed (at runtime), not variables, functions, etc. I think your interpretation of weak vs. strong is already covered by static vs. dynamic, whereas the other interpretation of weak typing is about silent coercion of types such as in Javascript and PHP where for example an integer can be concatenated to a string, or in C where a string can be interpreted as an integer with a cast.
- mixmastamyk 14y agoLooks exactly like namedtuple, why do we need this?
- reinhardt 14y agoThe only similarity is the signature; semantics are completely unrelated.
- mixmastamyk 14y agoI guess I'm looking for a usecase that namedtuple (or set...) doesn't support. Here's one example: http://pythonpath.wordpress.com/2012/01/26/python-enum-with-namedtuple/ http://pythonpath.wordpress.com/2012/01/26/python-enum-with-...
- shoo 14y agoin that example the values of the enumerated constants are simply integer values, they do not have their own type. so there is nothing stopping you comparing values from two unrelated enumerations, and getting a nonsensical True or False result, instead of a TypeError.
- mixmastamyk 14y agoI suppose that's true and theoretically useful. I don't find myself coding nonsense very often, but python has not been a language that gives much support (or is appropriate) to use cases that can't tolerate any. I now see some good uses for Enum, but (with a skeptical eye to additions) I believe that the reasons to keep the language/libs small are equally important.
- famousactress 14y agoAs an optional implementation that'd be nice, but this syntax I assume is meant to solve two problems: 1. Making the integral values of an enum clear and unchanged by re-ordering the names. If the integral values matter to you (because you serialize enums that way), then I would want them to be explicit. 2. The integer values of these enums aren't exclusive. Two enums can reference the same value. That said, I agree a short hand for people who don't care seems reasonable, where the integers could be auto-assigned by definition order, or just all set to 0.
- deleted 14y ago[deleted]
- encoderer 14y agoWould love to see functionality here similar to the iota keyword in Go.
- Tobu 14y agoThe syntax is easy for us who have been using ghetto enums: class Colors: red, green, blue = range(3)
- chrismorgan 14y agoAdding new values in the middle is exactly why it needs to be explicit. If you're pickling them, for example, or otherwise recording them, you certainly care about the values. Auto-assignment is often convenient, but it certainly must never be the only way to do it.