[Python-ideas] "Exposing" `__min__` and `__max__`

Jacco van Dorp j.van.dorp at deonet.nl
Wed Jun 27 02:43:25 EDT 2018

2018-06-26 17:34 GMT+02:00 Franklin? Lee <leewangzhong+python at gmail.com>:
> Caller detects: The caller checks length before calling the dunder. If there
> is no dunder, it doesn't check. Are there real-world cases where length is
> not defined on an iterable collection?

Generators dont have a __len__ method. And they might have min/max
that can be calculated without iterating over the entire thing. The
builtin range() is an example. (but also an exception, since it does
have a __len__ attribute. This is specifically part of range and not
generators in general, though.).

However, range() is an example where the dunders could be valuable -
max(range(1e7)) already takes noticable time here, while it's rather
easy to figure it out from start stop and step, just like len now does
for it.

More information about the Python-ideas mailing list