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

Jacco van Dorp j.van.dorp at deonet.nl
Wed Jun 27 03:59:11 EDT 2018

2018-06-27 9:36 GMT+02:00 Michael Selik <mike at selik.org>:
> On Tue, Jun 26, 2018, 11:43 PM Jacco van Dorp <j.van.dorp at deonet.nl> wrote:
>> 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.
> Have you ever written ``max(range(x))`` in production code?

Have you ever written len(range(x)) in production code ?

More information about the Python-ideas mailing list