<p>我有一个不同块的二维数组,都是从块继承的。我想检查我单击的块是否是污垢类型的块,如下所示:</p>
<pre><code>clickedblock = getClickedBlock()
if isinstance(clickedblock, Dirt):
place a block
else:
don't place a block
</code></pre>
<p>我听说<code>isinstance</code>不好,应该避免,因为它会在代码中创建分叉。使用<code>isinstance</code>什么时候比较好?在</p>
<p>对于我的问题,另一个更麻烦的解决方案是使用一个名为'id'的块字段,然后检查它是否等于某个常量,这意味着灰尘。但这听起来很糟糕,而且比简单的<code>isinstance</code>更容易出错。在</p>