5 ms·
I 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
by oellegaard 13y ago
I 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 13y agoWhat disgusts you about it, in particular?
- ekimekim 13y 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 13y 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 13y agoThe second parameter can also be a list/tuple of strings. e.g.: >>> Animal = Enum('Animal', ('lion', 'tiger', 'liger', 'tigon'))
- Tobu 13y agoStrange that the auto-assigned values start at one.
- w0utert 13y 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 13y 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 13y agoPython is a strongly-typed language. Perhaps you're thinking of static versus dynamic typing?
- Tobu 13y 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 13y 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.
- 13y ago
- mixmastamyk 13y agoLooks exactly like namedtuple, why do we need this?
- reinhardt 13y agoThe only similarity is the signature; semantics are completely unrelated.
- mixmastamyk 13y 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 13y 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 13y 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.