by oellegaard · 13y ago · 0 comments
>>> 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.
>>> Animal = Enum('Animal', ('lion', 'tiger', 'liger', 'tigon'))
It's weird, unnecessary, it just shouldn't be there.
Personally, I would've preferred:
>>> Enum('Example', 'foo', 'bar', 'baz')
(ie. Enum(name, *values))
* 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.
http://pythonpath.wordpress.com/2012/01/26/python-enum-with-...
Now all I want in Python is a real switch statement...
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.
Perhaps you're thinking of static versus dynamic typing?