Python:检查变量类型

标签 python isinstance

我有一个关于 python 的问题。

我有变量 abcd

我有以下行:

if    not isinstance(a, int)   or not isinstance(b, int)  \
   or not isinstance(c, int)   or not isinstance(d, int)  \
   or not isinstance(a, float) or not isinstance(b, float)\
   or not isinstance(c, float) or not isinstance(d, float):
    do something

是否可以缩短此代码?

谢谢!

最佳答案

你应该使用all:

if not all(isinstance(var, (int, float)) for var in [a, b, c, d]):
    # do stuff

请注意,您可以为 isinstance 调用提供 int 和“float”。

关于Python:检查变量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704455/

相关文章:

python - Spark+Python设置GC内存阈值

python - 更好的 assertEqual() 为 os.stat(myfile).st_mode

python - 如何在 Google App Engine 中打印?

python - 无法区分用于执行一项特定操作的两个选择器

python - 在 python 中,是否有与 isinstance 等效的静态?

Python 断言 isinstance() 向量

list - Jython 中的 isinstance

python - Python 的 Twisted Reactor 是如何工作的?

python - isinstance 函数的奇怪行为

python - Python open(file)的类型是什么