擅长:python、mysql、java
<p><code>__getattr__</code>的优点是只有在属性不存在时才调用它,因此不需要显式列表——任何未定义的内容都将自动获得代理。</p>
<p><code>__setattr__</code>更复杂,因为它总是被调用。确保在设置自己的属性时使用超类调用或<code>object.__setattr__</code>;在<code>__setattr__</code>内使用<code>setattr()</code>将导致无限递归。</p>
<p>最后一个位,影响isinstance,是非常困难的。您可以通过对包装器实例的.<code>__class__</code>变量(但这也会重写类字典的解析顺序)的辅助,或者通过使用元类动态构造包装器类型来完成此任务。由于isinstance在Python代码中非常罕见,所以实际上试图欺骗它似乎有些过头了。</p>
<p><a href="http://python.org/doc/2.2/ref/attribute-access.html" rel="nofollow noreferrer">More information on special attribute access methods.</a></p>
<p><a href="http://docs.python.org/reference/datamodel.html#customizing-class-creation" rel="nofollow noreferrer">Even more information on them, plus some help with metaclasses.</a></p>