Python: Default Comparison
Solution 1:
I think the only rationale is that it is convenient that objects can be sorted and e.g. used as dictionary keys with some default behavior. The relevant chapter in the language definition is here: https://docs.python.org/2/reference/expressions.html#not-in
"The choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program."
So the fact that objects are currently compared using the memory address is just an implementation detail that cannot be counted upon. The only guarantee is that the ordering stays consistent during execution.
Solution 2:
I'm not exactly sure, but maybe someone can correct me on this.
When you compare objects, it compares their memory address, think of comparing 2 cstrings in C. If you take a look, the sorting sorted the objects from the lowest memory address to the highest memory address (or pointer location).
Solution 3:
Look at the values when you print it. Notice the hex number next to the "Object C at"? That is a pointer reference. It can roughly be equated to creation order. If you iterate through that list, you'll see that it has sorted the objects using that as the standard.
As a side note, I remember being confused by Python 2.x comparisons... but I don't know if this, in particular, was fixed in Py3k.
Post a Comment for "Python: Default Comparison"