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
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com