Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

object - Python: default comparison

In Python 2.7, I define an empty new-style class:

In [43]: class C(object): pass
   ....:

then create a list of instances of the new class:

In [44]: c = [C() for i in xrange(10)]

then attempt to sort the list:

In [45]: sorted(c)
Out[45]:
[<__main__.C object at 0x1950a490>,
 <__main__.C object at 0x1950a4d0>,
 ...
 <__main__.C object at 0x1950aad0>]

What's surprising is that the sort doesn't complain, even though I haven't defined a way to compare instances of C:

In [46]: dir(C())
Out[46]:
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']

What exactly is happening there, and what's the rationale for this -- arguably surprising -- behaviour?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...