Should this be considered a bug in the Enum implementation?
class Foo(enum.Enum): ... A = True ... B = 1 ... C = 0 ... D = False ... Foo.A <Foo.A: True> Foo(True) <Foo.A: True> Foo(1) <Foo.A: True>
Seems to me like it should store and compare both type and value. Paul
Paul Bryan via Python-Dev writes:
Should this be considered a bug in the Enum implementation?
Probably not. The underlying implementation of Enums is integers, and False and True *are* the integers 0 and 1 for most purposes. And it propagates further. Same example:
class Foo(enum.Enum): ... A=True ... B=1 ... C=0 ... D=False ... Foo.B <Foo.A: True>
This amusing artifact was discussed in another thread recently.
IMO it's a feature, not a bug :-)
import enum class Foo(enum.Enum): ... A = 1 ... B = 1.0 ... Foo(1) <Foo.A: 1> Foo(1.0) <Foo.A: 1> Foo.B <Foo.A: 1>
See: https://docs.python.org/dev/library/enum.html#duplicating-enum-members-and-v... "However, two enum members are allowed to have the same value. Given two members A and B with the same value (and A defined first), B is an alias to A. By-value lookup of the value of A and B will return A. By-name lookup of B will also return A." You can use @unique to detect such corner case:
@enum.unique ... class Foo2(enum.Enum): ... A = 1 ... B = 1.0 ... ValueError: duplicate values found in <enum 'Foo2'>: B -> A
Victor On Mon, Dec 28, 2020 at 1:25 AM Paul Bryan via Python-Dev <python-dev@python.org> wrote:
Should this be considered a bug in the Enum implementation?
class Foo(enum.Enum):
... A = True
... B = 1
... C = 0
... D = False
...
Foo.A
<Foo.A: True>
Foo(True)
<Foo.A: True>
Foo(1)
<Foo.A: True>
Seems to me like it should store and compare both type and value.
Paul _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/5IJPHFRL... Code of Conduct: http://python.org/psf/codeofconduct/
-- Night gathers, and now my watch begins. It shall not end until my death.
participants (3)
-
Paul Bryan
-
Stephen J. Turnbull
-
Victor Stinner