A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2013-April/125535.html below:

[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard libraryBarry Warsaw barry at python.org
Thu Apr 25 21:15:12 CEST 2013
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
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4