Do you mind sharing an example usage in a realistic context? There might be a good solution that doesn't require adding magic methods.<div><br></div><div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 19, 2018, 12:24 PM James Edwards <<a href="mailto:jheiv@jheiv.com">jheiv@jheiv.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I've only recently looked for these special methods, so that in and of itself may be the reason these methods aren't exposed, but I could think of objects that may wish to implement __min__ and __max__ themselves, for efficiency. For example:<div><br></div><div><div><font face="monospace, monospace"> # A "self-sorted" list object</font></div><div><font face="monospace, monospace"> class AlwaysSortedListObejct:</font></div><div><font face="monospace, monospace"> def __min__(self): return self.lst[0]</font></div><div><font face="monospace, monospace"> def __max__(self): return self.lst[-1]</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> # An object that maintains indices of extrema (e.g. for complex comparisons)</font></div><div><font face="monospace, monospace"> class KeepsTrackOfExtrema:</font></div><div><font face="monospace, monospace"> def __init__(self):</font></div><div><font face="monospace, monospace"> self.min_index = None</font></div><div><font face="monospace, monospace"> self.max_index = None</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> def append(self, obj):</font></div><div><font face="monospace, monospace"> new_index = len(obj)</font></div><div><font face="monospace, monospace"> self.backer.append(obj)</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> if (self.max_index is None) or (obj > self.backer[self.max_index]):</font></div><div><font face="monospace, monospace"> self.max_index = new_index</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> if (self.min_index is None) or (obj < self.backer[self.min_index]):</font></div><div><font face="monospace, monospace"> self.min_index = new_index </font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> def __min__(self): return self.backer[self.min_index]</font></div><div><font face="monospace, monospace"> def __max__(self): return self.backer[self.max_index]</font></div><div> </div><div>Where these methods be called via the single-argument calls to `max(obj)` and `min(obj)`. </div><div><br></div><div>If it's not clear, it'd be similar to the way __len__ is called (when defined) via len(obj).</div><div><br></div><div>My solution was to implement a .min() method, but that caused some ugly special casing when the object could also be a regular list (where I'd want to iterate over all of the items).</div><div><br></div><div>I searched the list, but has this been discussed before? Is there any merit in it?</div><div> </div></div></div>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div>