Range Operation pre-PEP

Tim Peters tim.one at home.com
Sat May 12 02:20:19 CEST 2001

[Rainer Deyke]
> I can't think of a single function in the standard library which
> takes an unbounded number of heterogenous arguments.  I can think
> of several which take an unbounded number of homogeneous arguments.
> 'min' and 'max' for example.

> ?
> >>> import sys
> >>> min(1, 1.0, [1], "one", {1 :1}, sys.stdin)
> 1
> >>>
> I suppose those are homogeneous arguments in the sense that they're
> all objects, but if that's the degenerate sense we're using then I
> don't know what heterogeneous could mean.

[Alex Martelli]
> Isn't it (Python 2.1 at least) "homogeneous in the sense they're all

Ya, I suppose that's what Rainer did mean, because he's not in the habit of
making vacuous stmts and we have to pick *some* meaning for a word that isn't
normally used when discussing Python.  It's a peculiar meaning for
homogeneous, though, because it's impossible to say whether a collection of
objects is "homogeneous" in this sense, given only the collection of objects:
it depends also on the uses that will be made of them.  Then I still don't
like accepting it, because it reduces heterogeneous to meaning "some
collection of objects that *can't* be used in a given context" -- and
heterogeneous is clearly a wrong word for that.  If we're trying to talk
about conformance to a protocol, I strongly suggest using variants of
"conform" instead <wink>.

> ...
> Python doesn't formally define interfaces/protocols ("yet", he
> adds hopefully:-), but "informally" it has them -- here, it seems
> to me that the objects min/max accept need to "implement
> OrderedComparable" (be adaptable to protocol "compare by
> < &c") in the typical informal Python sense of interfaces and
> protocols/'implements' and 'adaptable to'...

Heh heh:  I just looked at the implementation.  Turns out that a given
collection of objects may be "homogeneous" for min() but not for max(), or
vice versa:  min() only invokes __lt__, max() only __gt__.  If Python needs
one of those but doesn't find it, it will try the other one with the
arguments swapped:

    class C:
        def __init__(self, i):
            self.i = i

        def __lt__(self, other):
            return self.i < other.i

        def __repr__(self):
            return "C(%d)" % self.i

    c6, c42 = C(6), C(42)
    print min(c6, c42)
    print max(c6, c42)



despite that only __lt__ is defined.  But give C a silly __gt__ too:

    def __gt__(self, other):
        raise ValueError("screw you!")

and it displays

    Traceback (most recent call last):
      File "minmax.py", line 16, in ?
        print max(c6, c42)
      File "minmax.py", line 12, in __gt__
        raise ValueError("screw you!")
    ValueError: screw you!

So the definition of "max homogeneous" gets correspondingly more convoluted.

not-to-say-useless-ly y'rs  - tim

More information about the Python-list mailing list