简体   繁体   English

Python中isinstance()的否定形式

[英]Negative form of isinstance() in Python

How would I use a negative form of Python's isinstance()? 我如何使用Python的负面形式isinstance()?

Normally negation would work something like 通常否定会起作用

x != 1

if x not in y

if not a

I just haven't seen an example with isinstance(), so I'd like to know if there's a correct way to used negation with isinstance(). 我只是没有看到isinstance()的一个例子,所以我想知道是否有一个正确的方法来使用isinstance()的否定。

Just use not . 只是not使用。 isinstance just returns a bool , which you can not like any other. isinstance只返回一个bool ,你not喜欢任何其他。

That would seem strange, but: 这看起来很奇怪,但是:

if not isinstance(...):
   ...

The isinstance function returns a boolean value. isinstance函数返回一个布尔值。 That means that you can negate it (or make any other logical operations like or or and ). 这意味着您可以否定它(或进行任何其他逻辑操作,如orand )。

Example: 例:

>>> a="str"
>>> isinstance(a, str)
True
>>> not isinstance(a, str)
False

Just use not , eg, 只是使用not ,例如,

if not isinstance(someVariable, str):
     ....

You are simply negating the "truth value" (ie Boolean) that isinstance is returning. 你只是否定了“真值”(即布尔)是isinstance正在恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM