[Python-Dev] enum instances
ethan at stoneleaf.us
Tue Apr 30 04:17:08 CEST 2013
On 04/29/2013 06:23 PM, Marco Hemmelrath wrote:
> First of all, hi, I'm new to this list.
> Following the enum discussions on this list I am kind of confused about how enums and their respective instances, i.e.
> the values, should behave in "normal" context.
> I apologize beforehand for the mass of "questions" because the following contains really many discussed things but they
> all kinda depend on one another, and for terminology which I might've missed.
> Considering we have:
> class Color(enum):
> red = 1
> white = 2
> other = "undefined"
> class State(enum):
> idle = 0
> busy = 1
> idling = idle
> ideling = 0
> together with the premises:
> 1. type(State.busy) == State
> 2. type(State) == enum
> 3. isinstance(State.idle, State)
> 4. State.idle is State.idle
> which should mostly be agreed on (if I didn't misinterpret).
> How would an enum instance (e.g. State.busy) behave in normal Python expressions? Should these instances just wrap their
> values and provide some simple overhead useful for enums?
> I'll just note down a few examples of how I think it could work and express a few thoughts on them:
> 1. State.busy == 1
> 2. State.busy == Color.red
> 3. int(State.Busy) is 1
> 4. isinstance(State.busy, int)
> 5. Color.other.startswith("und")
> 6. State.busy is not 1
> 7. State.busy is not Color.red
> 8. State.idle in State
> 9. 0 in State # True, False or raise?
> 10. State.idling is State.idle
> 11. State.idle == State.idling
> 12. State.idle is not State.idling
1 & 2 are False (would be true if using `.value`)
3 is True
4 & 5 are False (again, need `.value`)
6 is not the correct way to use `is` (should be `==`)
7, 8, 9, 10, 11 are True
12 is False
More information about the Python-Dev