[Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity (original) (raw)
Antoine Pitrou solipsis at pitrou.net
Fri May 3 10:51:03 CEST 2013
- Previous message: [Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity
- Next message: [Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Le Thu, 2 May 2013 14:57:35 -0700, Eli Bendersky <eliben at gmail.com> a écrit :
class Animal(Enum): values = 'cat dog' This is obviously a matter of preference (and hence bikeshedding), but this still looks better to me: Animal = Enum('Animal', 'cat dog') It has two advantages: 1. Shorter
You're gaining one line of code. I suppose it's significant if you write ten enums a day, otherwise... ;-)
2. Parallels namedtuple, which is by now a well known and widely used construct
namedtuple is the exception, not the rule. I don't know of another popular type which follows a similar scheme. On the other hand, well-known ORMs (SQLAlchemy, Django ORM) use a class-based syntax despite their declarative nature and the fact that they allow you to set "meta" options (e.g. the name of the reflected table).
As an egoistical data point, I always subclass namedtuples, because I minimally want to add a docstring, and sometimes I also want to add behaviour (e.g. alternate constructors, serialization). Which means namedtuple's declarative conciseness is generally lost for me :-)
Note that besides ORMs, the proposed values has built-in precedent with slots.
Regards
Antoine.
- Previous message: [Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity
- Next message: [Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]