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

Michael Selik mike at selik.org
Wed Jun 27 03:36:12 EDT 2018

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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180627/eb08fc10/attachment.html>

More information about the Python-ideas mailing list