[Python-ideas] Exploiting type-homogeneity in list.sort() (again!)
David Mertz
mertz at gnosis.cx
Tue Mar 7 18:53:18 EST 2017
On Tue, Mar 7, 2017 at 6:27 PM, Erik <python at lucidity.plus.com> wrote:
> Good point about list.extend(). I don't think __type_hint__ could help
>> with .__contains__() or .count() or .remove(). E.g.:
>>
>> In [7]: lst = [1.0, 2.0, 1+0j, F(1,1)]
>> In [8]: from fractions import Fraction as F
>> In [9]: lst = [1.0, 2.0, 1+0j, F(1,1)]
>> In [10]: 1 in lst
>> Out[10]: True
>>
>> The list has absolutely nothing of the right type. Yet it contains an
>> item, counts things that are equal, finds a position for an equal item.
>>
>
> Sure, but if the needle doesn't have the same type as the (homogeneous)
> haystack, then the rich comparison would still need to be done as a
> fallback (and would produce the result you indicate).
>
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")?
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170307/3d8802b2/attachment.html>
More information about the Python-ideas
mailing list