Can you assume that list of of type(list[0]) and use that type's optimised sort? But in the optimised sort code check that the types are as required. If you hit an element that is not of the required type then fall back to the unoptimised sort.
So part of list is sorted using optimised code and the sort is completed with the unoptimised code. Is that sematically clean? Barry > On 8 Mar 2017, at 17:08, Erik <[email protected]> wrote: > >> 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. > _______________________________________________ > Python-ideas mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > _______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
