3.3.10. 特殊方法查找

对于自定义类来说,特殊方法的隐式发起调用仅保证在其定义于对象类型中时能正确地发挥作用,而不能定义在对象实例字典中。 该行为就是以下代码会引发异常的原因。:

  1. >>> class C:
  2. ... pass
  3. ...
  4. >>> c = C()
  5. >>> c.__len__ = lambda: 5
  6. >>> len(c)
  7. Traceback (most recent call last):
  8. File "<stdin>", line 1, in <module>
  9. TypeError: object of type 'C' has no len()

此行为背后的原理在于包括类型对象在内的所有对象都会实现的几个特殊方法,例如 hash()repr()。 如果这些方法的隐式查找使用了传统的查找过程,它们会在对类型对象本身发起调用时失败:

  1. >>> 1 .__hash__() == hash(1)
  2. True
  3. >>> int.__hash__() == hash(int)
  4. Traceback (most recent call last):
  5. File "<stdin>", line 1, in <module>
  6. TypeError: descriptor '__hash__' of 'int' object needs an argument

以这种方式不正确地尝试发起调用一个类的未绑定方法有时被称为‘元类混淆’,可以通过在查找特殊方法时绕过实例的方式来避免:

  1. >>> type(1).__hash__(1) == hash(1)
  2. True
  3. >>> type(int).__hash__(int) == hash(int)
  4. True

除了为了正确性而绕过任何实例属性之外,隐式特殊方法查找通常也会绕过 getattribute() 方法,甚至包括对象的元类:

  1. >>> class Meta(type):
  2. ... def __getattribute__(*args):
  3. ... print("Metaclass getattribute invoked")
  4. ... return type.__getattribute__(*args)
  5. ...
  6. >>> class C(object, metaclass=Meta):
  7. ... def __len__(self):
  8. ... return 10
  9. ... def __getattribute__(*args):
  10. ... print("Class getattribute invoked")
  11. ... return object.__getattribute__(*args)
  12. ...
  13. >>> c = C()
  14. >>> c.__len__() # Explicit lookup via instance
  15. Class getattribute invoked
  16. 10
  17. >>> type(c).__len__(c) # Explicit lookup via type
  18. Metaclass getattribute invoked
  19. 10
  20. >>> len(c) # Implicit lookup
  21. 10

以这种方式绕过 getattribute() 机制为解析器内部的速度优化提供了显著的空间,其代价则是牺牲了处理特殊方法时的一些灵活性(特殊方法 必须 设置在类对象本身上以便始终一致地由解释器发起调用)。