Hacker News new | past | comments | ask | show | jobs | submit login

Sweet - a standardized "enum" module. I wonder if/when will Django adapt this in their Model definition?



In my opinion, the enum module will do very little to improve your life. It uses a great deal of machinery to solve a somewhat shallow problem.

Yes, there will be a few cases when it will improve code and help manage complexity. But it introduces its own complexities as well (i.e. learning two distinct enumeration types, serialization issues, etc).

One example of an unexpected complication is the effect on serialization. Python is having to change (and slow down) its JSON module just to accommodate integer enums (http://bugs.python.org/issue18264). Third-party modules will need to adapt as well. Another example is the awkward __module__ attribute that you will need to support pickles.

That said, the real issue is that for many everyday needs, it is hard to beat the simplicity of what people were already doing. For example, sometime you only need a couple of constant references into list positions: PREV=1; NEXT=2; link_next[PREV] = link_prev[NEXT]. Or you need a handful of self-describing constants that can be compared for equality: ROUND_DOWN = 'ROUND_DOWN'; ROUND_UP = 'ROUND_UP'. Both of these examples were taken from code in the standard library. Both work just fine and do not need any of the high-powered machinery in the enum module.

Once in a while, having an enum will be nice, but you can expect people to go wild with it and use them everywhere. This will churn your code without producing any visible benefit for end users. Just as with PEP 8, the enum module will attract fools like moths to a flame.

Do yourself a favor and don't push for enums to be used everywhere. Show restraint and save them for cases like the socket module where we really did need a way to manage the menagerie of constants.


Seconded - I just made a gushing sound that made my girlfriend think I saw a puppy online or something.

I may need help.


Note that you can actually use the same `enum` interface on Python 2.4+ now that it has been standardized. Just install from PyPI. :-) [1]

[1] - https://pypi.python.org/pypi/enum34




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: