Python:检查变量类型 [英] Python: checking type of variables
本文介绍了Python:检查变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对python有疑问.
I have a question about python.
我有变量a
,b
,c
和d
.
我有以下一行:
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
是否可以使这段代码更短?
Is it possible to make this code shorter?
谢谢!
推荐答案
U应该使用all
:
if not all(isinstance(var, (int, float)) for var in [a, b, c, d]):
# do stuff
请注意,您可以在isinstance
调用中同时提供int
和'float'.
Note, that you can supply both int
and 'float' to the isinstance
call.
这篇关于Python:检查变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文