[Python-ideas] Enums

Michael Foord fuzzyman at gmail.com
Mon Jul 25 21:09:15 CEST 2011


On 25 July 2011 19:47, Raymond Hettinger <raymond.hettinger at gmail.com>wrote:

>
> 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?
>
>
Nope. Just something like this at the module level.

IGNORECASE = Flags.IGNORECASE

What it gains you is a nicer representation when looking at the values when
debugging. On the other hand being able to have access to all the constants
namespaced on a single object (as well) doesn't seem like such a bad thing.

Michael



>
> * 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
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110725/775a4a62/attachment.html>


More information about the Python-ideas mailing list