Why does min(A,B) behave different for lists and for classes?

Claudio Grondi claudio.grondi at freenet.de
Fri Aug 26 16:31:11 EDT 2005


Trying to understand the outcome of the recent
thread (called later reference thread):

"Speed quirk: redundant line gives six-fold speedup"

I have put following piece of Python code together:

class PythonObject_class:
  pass
PythonObject_class_instanceA = PythonObject_class()
PythonObject_class_instanceB = PythonObject_class()
PythonObject_list_instanceA = [1]
PythonObject_list_instanceB = [1]

print "min(A,B) is A: "
print "in case of classes as parameter: " +
str(min(PythonObject_class_instanceA, PythonObject_class_instanceB) is
PythonObject_class_instanceA)
print "in case of lists as parameter:   " +
str(min(PythonObject_list_instanceA,  PythonObject_list_instanceB)  is
PythonObject_list_instanceA)
print "min(B,A) is A: "
print "in case of classes as parameter: " +
str(min(PythonObject_class_instanceB,
PythonObject_class_instanceA) is PythonObject_class_instanceA)
print "in case of lists as parameter:   " +
str(min(PythonObject_list_instanceB,
PythonObject_list_instanceA) is PythonObject_list_instanceA)

getting as output:

min(A,B) is A:
in case of classes as parameter: True
in case of lists as parameter:   True
min(B,A) is A:
in case of classes as parameter: True
in case of lists as parameter:   False

Is there any deeper reason I don't understand
explaining why does min(A,B) behave different
for classes than for lists?
It makes for me currently not much sense how it
behaves for classes, because the result is according
to the postings in the above mentioned reference
thread nondeterministic.

Claudio





More information about the Python-list mailing list