擅长:python、mysql、java
<p>Python的优点之一(正如许多人所说的,它的弱点)是它不依赖于编译时类型检查来实施接口。这意味着<em>不是</em>需要一组对象从一个公共基类继承来拥有相同的接口-它们仍然可以在任何函数中互换使用。这种行为通常称为<a href="http://en.wikipedia.org/wiki/Duck_typing#In_Python" rel="nofollow">duck typing</a>。在</p>
<p>事实上,因为Python是动态类型的,所以很难像您所说的那样“强制实现一致的接口”。因为这个原因,像<a href="http://wiki.zope.org/zope3/WhatAreInterfaces" rel="nofollow">zope.interface</a>这样的东西已经被创建了。如果所有<code>Model</code>类型都实现了一些常见的行为,那么您将从您的实例中从类中获得的主要好处是代码重用。在</p>
<p>更进一步,如果您在第三方库中有一些不相关的对象类型,而您希望将其用作<code>Model</code>,那么您甚至可以执行称为<a href="http://en.wikipedia.org/wiki/Monkey_patch" rel="nofollow">"monkey patching" or "duck punching"</a>的操作,以便添加提供<code>Model</code>接口所需的代码!在</p>