msg273780 - (view) |
Author: John Hagen (John Hagen) * |
Date: 2016-08-27 13:42 |
Many programming languages allow the developer to define a enum without specifying values when there would be no significance to those values. Adding some kind of support in the stdlib itself was rejected due to the high degree of magic that would be needed: https://bugs.python.org/issue26988 I propose that a simple example be added to the enum docs that show a developer how to use a normal Python Enum to create this most common and basic enum (without values). import enum class Color(enum.Enum): red = object() green = object() blue = object() object() returns a new unique value while conveying that that value should not be expected to be used in any meaningful way (unlike an integer value, which could be used to encode the Enum in some way). There is no extra magic going on here, no new code that needs to be added to the stdlib, and no bare identifiers that could confuse linters. For example, PyCharm already fully understands the above example and statically types it. This example also allows the developer to omit the extra @enum.unique() boilerplate that is normally needed, since he or she cannot accidentally use the same integer value twice. |
|
|
msg273818 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2016-08-28 08:47 |
After a little more thought, I think this would be a worthwhile recipe and would help communicate the notion that the value has no particular meaning. |
|
|
msg273835 - (view) |
Author: John Hagen (John Hagen) * |
Date: 2016-08-28 18:38 |
Raymond, thanks for your consideration and input. I'll work on a small patch unless I hear from Ethan that he'd rather do it. I'm happy to defer to his expertise. I did try out None as a value just to be sure that that didn't work as it would not be a bad alternative. But since the values are "equal", Enum tries to alias them. import enum @enum.unique class Color(enum.Enum): red = None green = None blue = None >>> ValueError: duplicate values found in <enum 'Color'>: green -> red, blue -> red |
|
|
msg273842 - (view) |
Author: John Hagen (John Hagen) * |
Date: 2016-08-28 21:03 |
Patch attached. |
|
|
msg273847 - (view) |
Author: Anilyka Barry (abarry) *  |
Date: 2016-08-28 23:41 |
The patch doesn't apply. I manually copy-pasted the lines in the source and generated a new one. I would probably rephrase "these values hold no meaning and should not be used" into "the values are not important" or something along those lines. |
|
|
msg273854 - (view) |
Author: John Hagen (John Hagen) * |
Date: 2016-08-29 11:28 |
Emanuel, I like your rewording. Uploaded a new patch incorporating it. |
|
|
msg274818 - (view) |
Author: Ethan Furman (ethan.furman) *  |
Date: 2016-09-07 15:19 |
New changeset 871bdb06c1cf by Ethan Furman in branch 'default': add recipes for pseudo-valueless enums https://hg.python.org/cpython/rev/871bdb06c1cf |
|
|
msg274821 - (view) |
Author: Ethan Furman (ethan.furman) *  |
Date: 2016-09-07 15:23 |
Thanks for the patch, John and Emanuel! |
|
|