On Tue, Dec 5, 2017 at 8:54 AM, Julien Salort <lis...@salort.eu> wrote: > Le 04/12/2017 à 14:16, Steven D'Aprano a écrit : > >> We're taking something which belongs in the report generator or >> collection, the knowledge of how to sort a collection of unordered >> values, and baking it into the values themselves. (Effectively making >> them ordered!) > > It is also possible to use this __key__ method for classes for which the > ordering > is indeed unambiguously defined, e.g.: > > class MyValue: > > def __init__(self, value, comment): > self.value = value > self.comment = comment > > def __key__(self): > return self.value > > Then it is not shocking to define a sorting key.
MyValue = namedtuple('MyValue', ['value', 'comment']) Job done :) ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/