[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library (original) (raw)
Barry Warsaw barry at python.org
Thu Apr 25 21:15:12 CEST 2013
- Previous message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Next message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Apr 23, 2013, at 04:33 PM, Antoine Pitrou wrote:
That said, I don't see why it wouldn't make sense for an enum value to be an instance of that class. It can be useful to write
isinstance(value,_ _MyEnumClass)
. Also, any debug facility which has a preference for writing out class names would produce a better output than the generic "EnumValue".
These semantics seem very weird to me, but at least we have a principled way to lie about it in Python 3. We could add this to the metaclass:
def __instancecheck__(cls, instance):
return instance.enum is cls or cls in instance.enum.__bases__
Thus:
>>> X = Enum('X', 'a b c')
>>> Y = Enum('Y', 'z y x')
>>> class Z(Y):
... d = 4
... e = 5
...
>>> isinstance(Z.d, Y)
True
>>> isinstance(Z.d, Z)
True
>>> isinstance(Z.d, X)
False
>>> isinstance(Y.z, Y)
True
>>> isinstance(Y.z, Z)
False
-Barry
- Previous message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Next message: [Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]