[Python-ideas] Exploiting type-homogeneity in list.sort() (again!)

Erik python at lucidity.plus.com
Wed Mar 8 12:08:51 EST 2017


On 08/03/17 11:07, Steven D'Aprano wrote:
> I mentioned earlier that I have code which has to track the type of list
> items, and swaps to a different algorithm when the types are not all the
> same.

Hmmm. Yes, I guess if the expensive version requires a lot of 
isinstance() messing or similar for each element then it could be better 
to have optimized versions for homogeneous lists of ints or strings etc.

>> A list.is_heterogeneous() method
>> could be implemented if it was necessary,
>
> I would prefer to get the list item's type:
>
> if mylist.__type_hint__ is float:

If you know the list is homogeneous then the item's type is 
"type(mylist[0])".

Also, having it be a function call gives an obvious place to put the 
transition from "unknown" to known state if the tri-state hint approach 
was taken. Otherwise, that would have to be hooked into the attribute 
access somehow.

That's for someone who wants to try implementing it to decide and 
propose though :)

E.


More information about the Python-ideas mailing list