classification
Title: Add base for enumerations (Functional API)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, dkorchem, eli.bendersky, ethan.furman, python-dev, terry.reedy
Priority: normal Keywords:

Created on 2014-06-10 14:33 by dkorchem, last changed 2014-09-18 02:22 by python-dev. This issue is now closed.

Messages (8)
msg220169 - (view) Author: Dmitry Korchemny (dkorchem) Date: 2014-06-10 14:33
In enum module the functional API for enum creation has the following signature:
Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>)

so that the numeration always starts with 1. In some cases it is convenient to start numbering from other base, e.g., 0. It would be of great help to add an additional parameter, say start, to make the following call possible:

Animal = Enum('Animal', 'ant bee cat dog', start = 0)
msg220209 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2014-06-10 23:15
That is certainly nicer than the current idiom:

  Animal = Enum('Animal', zip('ant bee cat dog'.split(), range(4)))
msg220210 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2014-06-10 23:20
Is it really worthwhile to complicate the API for the sake of providing a less flexible solution for rare cases that saves a few keystrokes?
msg220212 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2014-06-10 23:34
Playing devil's advocate:

The issue is not so much the keystrokes saved as the improvement in reading and understanding what was intended.  If you are happy with starting at 1 the idiom is easy to both write, read, and understand; but if you want some other starting number you have to uglify your code with parenthesis, .methods, and add an extra iterator which you have to manually re-sync if you later add another name...

I think that is the real issue -- not keystrokes.
msg220237 - (view) Author: Dmitry Korchemny (dkorchem) Date: 2014-06-11 06:22
I think that the situation when you want start numbering from 0 is rather common, especially when you need to define bit fields as enumeration or when you need to implement an interface with other languages (e.g., C).
msg220473 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-06-13 17:55
Looks sensible to me. Except for being name-only, this duplicates the api of enumerate.
msg226975 - (view) Author: Roundup Robot (python-dev) Date: 2014-09-17 03:36
New changeset ec016ba862ba by Ethan Furman in branch 'default':
Close issue21706: add 'start' parameter to functional API
https://hg.python.org/cpython/rev/ec016ba862ba
msg227024 - (view) Author: Roundup Robot (python-dev) Date: 2014-09-18 02:22
New changeset 713ee49ec3ba by Berker Peksag in branch 'default':
Issue #21706: Add a versionchanged directive to the functional API docs.
https://hg.python.org/cpython/rev/713ee49ec3ba
History
Date User Action Args
2014-09-18 02:22:33python-devsetmessages: + msg227024
2014-09-17 03:36:23python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg226975

resolution: fixed
stage: resolved
2014-06-13 17:55:58terry.reedysetnosy: + terry.reedy
messages: + msg220473
2014-06-11 06:22:40dkorchemsetmessages: + msg220237
2014-06-10 23:34:22ethan.furmansetmessages: + msg220212
2014-06-10 23:20:26eli.benderskysetmessages: + msg220210
2014-06-10 23:15:57ethan.furmansetassignee: ethan.furman
messages: + msg220209
2014-06-10 14:56:58zach.waresetnosy: + barry, eli.bendersky, ethan.furman
2014-06-10 14:33:48dkorchemcreate