【发布时间】:2021-02-20 13:19:07
【问题描述】:
我是 Python 的初学者...目前我正在尝试编写一个函数,它首先检查输入 x 和 y 是 int 还是 float。
我猜这个工作是什么
if (type(x) != int and type(x) != float) or (type(y) != int and type(y) != float):
但是,这对我来说似乎很笨拙/效率低下,并且在有很多输入的情况下很难概括。因此我认为应该有一种更优雅的方式来编写这个条件..?感谢您的任何想法!
【问题讨论】:
-
使用
isinstance()。 -
非常感谢您的回答,isinstance() 函数正是我想要的!
标签: python function if-statement types