请考虑以下代码:
>>> class X:
... pass
...
>>> x = X()
>>> len(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'X' has no len()
>>> bool(x)
True
__len__
它不起作用。
>>> class Y:
... def __len__(self):
... raise TypeError
...
>>> y = Y()
>>> len(y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __len__
TypeError
>>> bool(y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __len__
TypeError
有没有办法写一个
像这样工作的函数没有实现?