Emanuel Barry added the comment:

I tend to like all things magic, but the more I think about it, and the less I 
like it being a part of the standard library. I've had a use for this feature 
before, and when I did, I cooked my own 12-lines subclass of EnumMeta and 
_EnumDict. Raymond's points are pretty spot-on, and I also think that this 
shouldn't go in the stdlib. There's still time.

While this particular flavour of magic sounds too, well, magic, for Python or 
even myself, I think a less magical approach can be taken. Namely, something 
like AutoNumberedEnum which requires values to be empty tuples is being 
explicit about what you want and not allowing typos in the class definition. 
Raymond's point about (temporarily) commenting out enum members breaking the 
order highlights this, and while this approach doesn't solve it, it makes it 
obvious that there is *something* that changed.

Another approach, which doesn't exclude the above, is to make EnumMeta and 
_EnumDict public and documented classes (!), thus allowing people like me to 
create their own blueberry-flavoured magic enumerations without any impact on 
the people who don't use my code. Or if I don't feel like reinventing the 
wheel, I can just pip install the module and use that instead.

I think that the whole idea of making enums in Python work like they do in C is 
looking at the problem from the wrong angle. It's true that Python takes some 
of its design from C, but naively trying to replicate C-like behaviour with 
C-like syntax doesn't work all the time. How often do beginners think 'x ^ y' 
means 'x to the power of y', and are then surprised by the behaviour?

I think this version of Enum raises the barrier to entry for new programmers. 
Enum is a nice feature, and it helps new and old programmers alike write 
clean(er) code for various purposes. When a programmer sees this use, they 
won't think "oh this must call __getitem__ and then assign an automatic value", 
they'll wonder "why is this code even running?". And then it's up to the 
Raymonds of this world to explain that Enum uses a metaclass (which, I'm sure, 
is not a topic they'll want to tackle by the time these programmers reach Enum) 
and that the mapping overloads __getitem__.

All in all, this magical approach is just too magical for Python. I understand 
that magic is fun to have and play with, but the standard libary isn't where 
you should keep your toys. I use a throwaway repo for all my magic 
this-is-not-a-good-idea-but-I-wanna-do-it-anyway ideas, and this is where I 
think such magic goes. It definitely doesn't belong in the standard library, 
within an arm's reach of the first curious programmer to wander there.

----------
nosy: +ebarry

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26988>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to