Negative form of isinstance() in Python

折月煮酒 提交于 2021-01-18 07:38:31

问题


How would I use a negative form of Python's 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().


回答1:


Just use not. isinstance just returns a bool, which you can not like any other.




回答2:


That would seem strange, but:

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

The isinstance function returns a boolean value. That means that you can negate it (or make any other logical operations like or or and).

Example:

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



回答3:


Just use not, e.g.,

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

You are simply negating the "truth value" (ie Boolean) that isinstance is returning.



来源:https://stackoverflow.com/questions/11748671/negative-form-of-isinstance-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!