New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose an Enum object's serial number #66695
Comments
I'd like Enum objects to expose their serial numbers. Currently it seems the only way to get this is Perhaps we can use |
On Sep 27, 2014, at 02:40 PM, Ram Rachum wrote:
Can you please provide some motivating use cases? |
Right now I want it for this: http://bugs.python.org/issue22504 Another use case I can think of is that if you store enum values in a database, you're probably using an int field and you'd want to easily convert between an enum and it's int value. |
On Sep 27, 2014, at 02:59 PM, Ram Rachum wrote:
https://docs.python.org/3/library/enum.html#orderedenum
Why would you do that for non-int enum values? There's lots of ways you could |
As I said in the other ticket: I can easily use a subclass, but I think it's general enough functionality for it to be included in the standard library. I could continue the discussion about databases, but it feels like a waste of time to me. The main principle is: If something has an important property (in this case an enum object's numerical value), it should be publicly exposed. Period. No need to spend hours discussing if and how that property will be used. They always end up getting used somehow-- The only question is whether the people using them need to obtain them through private variables for years until the developers finally understand that the property needs to be exposed publicly. If you want to go through that, be my guest. |
On Sep 27, 2014, at 04:15 PM, Ram Rachum wrote:
I think this is a misunderstanding. Only IntEnum members have a defined Enum members are also defined to be unordered, so their serial number is Let me say specifically that I am opposed to int() for coercion for >>> from enum import Enum
>>> class Colors(Enum):
... a = 'a'
... b = 'b'
... c = 'c'
...
>>> Colors.a is Colors.b
False
>>> Colors.a is Colors.a
True I think using IntEnums or a subclass to provide a convenient wrapper around |
Are you sure? The documentation says "Enumerations support iteration, in definition order" and shows how
I don't understand why it's a misnomer in the case you showed. |
Yes, we're sure. ;) Enums have a definition order to aid in the use-case of auto-numbering, and to make displays consistent. However, the basic Enum type is unordered. I do not see a serial number as being an intrinsic property of an enum -- outside of auto-numbering (also not available in the stdlib), what difference does it make what order it was defined in? What matters is the name and the value. If you want your enum to have a serial number you can easily add that functionality in. |
This kind of attitude is not welcome in the core Python development community. Please keep the discussion courteous and stick to technical arguments. FWIW I fully agree with Barry and Ethan here. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: