[Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity

Antoine Pitrou solipsis at pitrou.net
Fri May 3 10:51:03 CEST 2013


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.




More information about the Python-Dev mailing list