On Thu, Sep 5, 2013 at 2:37 PM, Ben Finney <ben+pyt...@benfinney.id.au> wrote: > Javier Candeira <jav...@candeira.com> writes: >> I have a sorted list. > > What do you mean? Is this a Python builtin list that you have sorted? If > not, where did this ‘sorted_list’ come from?
Yes, sorry, it's my own SortedList where I have overloaded __init__() and append() so contents have a 'sorted order' invariant. I thought it was clear, because I had written earlier that this typing issue crops up when writing the following simple year 1 data structures for my students: binary search tree, ordered list, heap, etc... >> But if I put in an incompatible/unorderable type, I get a TypeError: >> not comparable. > > Who is “I” in all this? There are multiple points of responsibility > being discussed, and I don't know which one you're identifying with by > “I”. "I" there is the user of the list. I was describing how the list works. Better said: whoever uses the list will get a "TypeError: unorderable types" exception if they attempt to put into the same instance of SortedList, for instance, a tuple and a string. Or an int and a list. Or any pair of items belonging to types that don't have a well defined reciprocal ordering. At risk of repeating myself: - in the naive implementation, the TypeError will arise on insertion, when the first functional comparison is made: if newitem > existingitem. - in the guarded implementation, the code checks that, if there already items in the list, at least the new item is orderable with regards to the one in li[0], and then raises its own TypeError exception manually (allowing for a structure-specific error message like "TypeError: unorderable types in sorted list:"). - in the Lars implementation, there is a comparison at the top of the method, that way the error is raised by Python, but at a part where it's not confusing because it's not down in the guts of the method, so it can't be any other type of functional bug. I don't think there's any way for an OrderedList.insert(self, item) method not to throw a TypeError exception in these circumstances. Testing may tell you how not to raise TypeErrors when *using* this SortedList, but it doesn't tell you which of the options to choose when *coding* a SortedList. Sorry for the confusing "I", I hope this makes my point clearer. J _______________________________________________ melbourne-pug mailing list melbourne-pug@python.org https://mail.python.org/mailman/listinfo/melbourne-pug