
Well, it's not an object of type 'type' like the list or dict mentioned by the OP, for which len() give a TypeError: object of type 'type' has no len() Any derived class of Enum will also return True for isinstance(..., type). Should the derived classes then neither have a meaningful result for len()? Enum and derived classes hold a container with a fixed set of values. It makes perfectly sense to ask for the number of possible values, even when there are none. Op 3/04/2023 om 08:53 schreef Chris Angelico:
On Mon, 3 Apr 2023 at 15:54, Benedict Verhegghe <bverheg@gmail.com> wrote:
Enum is not a type, like the OP suggested:
Well, it is:
isinstance(Enum, type) True
but it has a metaclass, meaning that the type of Enum is a subclass of type.
type(Enum) <class 'enum.EnumType'> type(Enum).__bases__ (<class 'type'>,)
I mean, we wouldn't be subclassing it if it weren't a type.
The __len__ method is implemented on the metaclass to allow all Enum subclasses to have lengths (ditto __iter__ to make them iterable), and as a consequence of that, Enum itself has all the attributes that it wants to give to its subclasses.
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/V2HVHK... Code of Conduct: http://python.org/psf/codeofconduct/