[Python-ideas] Enums

Raymond Hettinger raymond.hettinger at gmail.com
Mon Jul 25 20:47:46 CEST 2011


On Jul 25, 2011, at 1:06 PM, Michael Foord wrote:
> 
> Python standard library modules currently using integers for constants: 
> 
> * re - has flags (OR'able constants) defined in sre_constants, each flag has two names (e.g. re.IGNORECASE and re.I) 

What is being proposed here?  Will there be a new namespace so that we would start writing re.Flags.IGNORECASE instead of re.IGNORECASE?  Are module level constants now going to be considered bad-form?

If constants switch from re.IGNORECASE to re.flags.IGNORECASE, are there any benefits to compensate for being both wordier and slower?


> * os has SEEK_SET, SEEK_CUR, SEEK_END - *plus* those implemented in posix / nt 

Ditto

> * doctest has its own flag system, but is really just using integer flags / constants (quite a few of them) 
> * token has a tonne of constants (autogenerated) 
> * socket exports a bunch of constants defined in _socket 
> * gzip has flags: FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT 
> 
> * errno (builtin module) 
> 
> EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, 
> ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED 

It seems to me that an enum module or class doesn't make any of this code better.
AFAICT enums are a fat solution to a thin problem.

Since python makes it so easy to put constants in class namespaces
and module namespaces, there is much less need for enums than in
statically compiled languages.  I think the perceived need is really
more of a carry-over coding habit than an actual need.  

That being said, this proposal seems to have momentum,
so it is going happen anyway.  Once introduced, people will
think we've endorsed enums as the one right way to code
constants, so we will start to see them everywhere.  
Expect enums to become ubiquitous.


Raymond
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110725/9ba46f85/attachment.html>


More information about the Python-ideas mailing list