[Python-ideas] "Exposing" `__min__` and `__max__`
Michael Selik
mike at selik.org
Wed Jun 27 09:52:14 EDT 2018
On Wed, Jun 27, 2018, 3:06 AM Steven D'Aprano <steve at pearwood.info> wrote:
> 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.
>
My intent was to ask where a range was in fact passed into max, not merely
where it could be. It'd be enlightening to see a complete, realistic
example.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180627/c8f60101/attachment.html>
More information about the Python-ideas
mailing list