E271:不比较类型,请使用isinstance()

2024-05-01 22:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

python PEP 8 linter不喜欢这样:

assert type(a) == type(b)

它告诉我改为使用“isinstance()”。但是要使用isinstance我必须做如下的事情

^{pr2}$

这似乎更不明智,我不太明白这一点。在

林特在某种程度上是我看不见的智慧吗?或者说我是不是在某种程度上是智者看不见的?在


Tags: lintertypeassert事情pepisinstancepr2智者
1条回答
网友
1楼 · 发布于 2024-05-01 22:24:45

根据注释中添加的上下文:

according to my program's logic, one should have type(a) == type(b) at this point in the code, and I just want to assert that to see that everything is running smoothly

在这种情况下,您应该忽略掉linter,因为它对您没有任何有用的建议。E271旨在通过类型检查警告人们有关问题,例如:

if type(a) == A:
    ...

上面的例子可能是由于忽略了aA的子类的一个实例的可能性而意外地调试了逻辑流。在

相关问题 更多 >