>>> class B():
... pass
...
>>> class C(object):
... pass
...
>>> b = B()
>>> b.__len__ = lambda: 7
>>> len(b)
7
>>> c = C()
>>> c.__len__ = lambda: 5
>>> len(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len()
在Python's documentation中提及,但我仍然不了解它是如何运作的。