擅长:python、mysql、java
<p>我学会了不使用它的艰难方法。问题是结果对类定义的导入方式很敏感:</p>
<ul>
<li>在对象被实例化的地方</li>
<li>在执行<code>isinstance</code>测试的地方</li>
</ul>
<p>如果一个导入是相对的,另一个是绝对的-检查将失败。
本质上,这就像检查<code>SomeClass</code>与<code>somepackage.SomeClass</code>之间的相等性。从档案上看也不一样。另外,如果您的根目录和<code>somepackage</code>目录都在<code>PYTHONPATH</code>中,也会出现类似的结果,那么绝对样式的导入可以表示来自其中一个“源根”的路径,因此两个不同的绝对样式导入将导致实例检查失败。在</p>
<p>有人可能会争论,好的做法无论如何都会阻止这种情况的发生,但良好的做法在很大程度上也是关于不冒险的。本着这种精神,我更喜欢在一个公共祖先类中放一些抽象方法,这样我以后就可以依赖于事情是如何嘎嘎作响的,而不是依赖于解释器认为它们是什么。在</p>
<p>在Java中,每个类解析为其完全限定的类名。这些在程序中是独一无二的,对实例的测试是轻而易举的。在Python中,这些可能很滑。在</p>