[Python-ideas] Exploiting type-homogeneity in list.sort() (again!)
Elliot Gorokhovsky
elliot.gorokhovsky at gmail.com
Tue Mar 7 18:59:17 EST 2017
On Tue, Mar 7, 2017 at 4:53 PM David Mertz <mertz at gnosis.cx> wrote:
>
>
> In [22]: class Eq(int):
> def __eq__(self, other):
> return True
> ....:
> In [23]: four, five, six = Eq(4), Eq(5), Eq(6)
> In [24]: lst = [four, five, six]
> In [25]: lst.count(Eq(7))
> Out[25]: 3
>
>
> How would this work (other than saying "don't do that it's perverse")?
>
There would be two needless checks in the equality testing. First,
PyObject_RichCompareBool would see if other is a subclass of self, in which
case other->tp_richcompare would be used iff. it is non-null. Otherwise, we
would check if self->tp_richcompare is non-null, the check would pass, and
we would call self.__eq__. See the flow chart on my poster (linked to in
the first email on this thread).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170307/8f4daf71/attachment.html>
More information about the Python-ideas
mailing list