type(number)==(float或int)返回false python

-3 python

我正在检查函数的参数是浮点数还是带有此行的int并且它保持返回false,有人可以解释为什么会发生这种情况吗?

def distance_from_zero(number):
    if type(number) == (int or float):
        return abs(number)
    else:
        return "Nope"
Run Code Online (Sandbox Code Playgroud)

Jac*_*IRR 5

这是最短,最pythonic的方式.

用途isinstance:

if isinstance(number, (int, float)):
Run Code Online (Sandbox Code Playgroud)

  • 我说OP首先做的不是Pythonic.鸭子打字更好. (2认同)