On Mon, 3 Apr 2023 at 17:57, Benedict Verhegghe <bver...@gmail.com> wrote:
>
> 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()

I'm not sure what you mean here. Enum is of type EnumMeta, which is a
subclass of type. That means that, according to the normal rules of
type hierarchy, Enum is indeed a type.

Types add functionality that the parent type (with some Exceptions),
so it should be no surprise that EnumMeta can add a __len__ method
that type itself didn't have.

> Any derived class of Enum will also return True for
> isinstance(..., type).

Yes, that is correct. Any subclass of Enum will also be a type, and
will have a length.

>>> class TestEnum(Enum):
...     SPAM = 1
...     HAM = 2
...
>>> isinstance(TestEnum, type)
True
>>> len(TestEnum)
2

> 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.
>

Yeah, and I'm of the opinion that it's not a problem for Enum to have
zero possible values, but to have the concept of values. Although it
also wouldn't be a problem if it didn't.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/7BC4WQ5SN5AJBWOOWOUVWW7JK2RUSNDQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to