[Python-ideas] "Exposing" `__min__` and `__max__`
jheiv at jheiv.com
Wed Jun 27 21:45:05 EDT 2018
> I'd want to see code that currently uses ``if isinstance`` to switch
> between ``max(x)`` and ``x.max()``.
I understand that, and I've spent a few hours searching github with
less-than-stellar results due to github's search syntax ignoring '.' and '('
(That being said, there are a number of projects
<https://github.com/search?l=Python&q=__min__&type=Code> on github that
seem to expect __min__ to work (example
but it's true that some are using the dunder for their own purposes.).
However, there are many modules that implement objects that could make good
use of exposing __min__ (e.g. bintrees <https://pypi.org/project/bintrees/>,
bintrees provides `min_item()` and `max_item()` accessors.
sortedcontainers doesn't seem to explicitly provide similar methods, but
maintain the sortedness of the objects so min and max can be accessed via
And I can't stress enough the value in being able to switch to one of these
classes from a standard iterable and have the rest of your code (e.g.
`min()`s "just work").
Also, the desire for custom implementations is there
but the proposed solutions rely on (IMO) ugly hacks that would break other
The search for the perfect `x.min() if isinstance(...) else min(x)` still
On Wed, Jun 27, 2018 at 11:30 AM, Michael Selik <mike at selik.org> wrote:
> On Wed, Jun 27, 2018 at 8:16 AM Franklin? Lee <
> leewangzhong+python at gmail.com> wrote:
>> On Wed, Jun 27, 2018, 10:31 Steven D'Aprano <steve at pearwood.info> wrote:
>>> On Wed, Jun 27, 2018 at 06:52:14AM -0700, Michael Selik wrote:
>>> > My intent was to ask where a range was in fact passed into max, not
>>> > where it could be. It'd be enlightening to see a complete, realistic
>>> > example.
>>> A complete, realistic example is as I said: you call max() on some
>>> object which you don't control, the caller does. You could be
>>> passed a list, or a set, or a bitset, a binary search tree, a range
>>> object, whatever the caller happens to pass to you.
>> Let's just assume Michael wants to know, and isn't making an argument
>> against the proposal.
> I do want to know, but it's also an argument against the proposal -- that
> no one has contributed in-context usage to demonstrate the value. I'd want
> to see code that currently uses ``if isinstance`` to switch between
> ``max(x)`` and ``x.max()``. Chris Barker explained the issue well.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas