特殊方法查找如何工作?

时间:2014-08-23 15:27:08

标签: python

>>> 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中提及,但我仍然不了解它是如何运作的。

0 个答案:

没有答案