[Python-Dev] Re: Re: lists v. tuples

Andrew Koenig ark@research.att.com
16 Apr 2003 11:20:52 -0400


>> So the first time you care is the first time f(x, y) returns nonzero.
>> Now you can find out what kind of function f is by calling f(y, x).
>> If f(y, x) returns zero, f is <.  Otherwise, it's a 3-way comparison.

Guido> Right.  There's no flaw in this logic, but I'd hate to have to
Guido> explain it over and over...  I don't want people to believe
Guido> that Python can somehow magically sniff the difference between
Guido> two functions; they might expect it in other contexts.

I can understand your reluctance -- I was just pointing out that
it's possible.

However, I'm slightly dubious about the x.sort(lt=f) vs x.sort(cmp=f)
technique because it doesn't generalize terribly well.

If I want to write a function that takes a comparison function as an
argument, and eventualy passes that function to sort, what do I do?
Something like this?

        def myfun(foo, bar, lt=None, cmp=None):
                # ...
                x.sort(lt=lt, cmp=cmp)
                # ...

and assume that sort will use None as its defaults also?  Or must I
write

                if lt==None:
                        x.sort(cmp=cmp)
                else:
                        x.sort(lt=lt)

Either way it's inconvenient.

So I wonder if it might be better, as a way of allowing sort to take
two different types of comparison functions, to distinguish between
them by making them different types.

-- 
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark