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

Steven D'Aprano steve at pearwood.info
Wed Jun 27 06:01:02 EDT 2018


On Wed, Jun 27, 2018 at 12:36:12AM -0700, Michael Selik wrote:
> On Tue, Jun 26, 2018, 11:43 PM Jacco van Dorp <j.van.dorp at deonet.nl> wrote:

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

range is not a generator.


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

I have never written that.

But I have written ``max(iterable)`` dozens of times, where iterable 
could be a range object.



-- 
Steve


More information about the Python-ideas mailing list