gpt4 book ai didi

Python:检查变量类型

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:08 24 4
gpt4 key购买 nike

我有一个关于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com