我找到了一些旧的Python代码,它做的事情类似于:
if type(var) is type(1):
...
按预期,`pep8` 对此提出了抱怨,建议使用 `isinstance()`。但是问题在于 `numbers` 模块从 Python 2.6 才开始存在,而我需要编写能够与 Python 2.5+ 兼容的代码。因此 `if isinstance(var, Numbers.number)` 并非解决方案。在这种情况下,什么才是正确的解决方案呢?我找到了一些旧的Python代码,它做的事情类似于:
if type(var) is type(1):
...
按预期,`pep8` 对此提出了抱怨,建议使用 `isinstance()`。但是问题在于 `numbers` 模块从 Python 2.6 才开始存在,而我需要编写能够与 Python 2.5+ 兼容的代码。因此 `if isinstance(var, Numbers.number)` 并非解决方案。在这种情况下,什么才是正确的解决方案呢?在Python 2中,您可以使用types
模块:
>>> import types
>>> var = 1
>>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
>>> isinstance(var, NumberTypes)
True
注意使用元组来测试多种类型。
在底层,IntType
只是 int
的别名,等等:
>>> isinstance(var, (int, long, float, complex))
True
complex
类型要求你的Python编译时支持复数;如果你想进行防范,可以使用try/except语句块:
>>> try:
... NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
... except AttributeError:
... # No support for complex numbers compiled
... NumberTypes = (types.IntType, types.LongType, types.FloatType)
...
或者如果您直接使用这些类型:>>> try:
... NumberTypes = (int, long, float, complex)
... except NameError:
... # No support for complex numbers compiled
... NumberTypes = (int, long, float)
...
在Python 3中,types
不再具有任何标准类型别名,complex
始终启用,不再有long
和int
之间的差异,因此在Python 3中始终使用:NumberTypes = (int, float, complex)
最后但并非最不重要的,您可以使用numbers.Numbers
抽象基础类型(自Python 2.6起新增)来支持不直接继承上述类型的自定义数值类型:
>>> import numbers
>>> isinstance(var, numbers.Number)
True
检查函数同时也返回 decimal.Decimal()
和 fractions.Fraction()
对象的值为True
。
这个模块默认假设complex
类型是可用的;如果不可用则会导致导入错误。
numpy.int32
没有被 int
捕获。 - Matthias Arrasisinstance(np.int32(32), numbers.Integral)
是正确的。 - Martijn PietersPython 2支持4种数字类型int
、float
、long
和complex
,而Python 3.x
只支持3种类型:int
、float
和complex
>>> num = 10
>>> if isinstance(num, (int, float, long, complex)): #use tuple if checking against multiple types
print('yes it is a number')
yes it is a number
>>> isinstance(num, float)
False
>>> isinstance(num, int)
True
>>> a = complex(1, 2)
>>> isinstance(a, complex)
True
# import numbers
isinstance(var, numbers.Number)
判断var
是否为数字。示例:
import numbers
var = 5 ; print(isinstance(var, numbers.Number)) # True
var = 5.5; print(isinstance(var, numbers.Number)) # True
var = 'a'; print(isinstance(var, numbers.Number)) # False
import numpy as np
var = np.float128(888); print(isinstance(var, numbers.Number)) # True
class C: pass; var = C(); print(isinstance(c, numbers.Number)) # False
根据您使用的情况,鸭子类型可能是更好的方法(它肯定 常见 推荐)。Martijn Pieters的方法存在问题,您的列表中总会漏掉某些数字类型。就我所知,您的代码将无法处理:sympy有理数、任意精度整数以及任何实现复数的实现。
另一种选择是编写此类函数:
def is_number(thing):
try:
thing + 1
return True
except TypeError:
return False
这段代码应该适用于任何合理的数字实现。当然,有一个主要缺点:它也可以与大量非数字的不合理实现一起使用(即如果加号运算符被重载并接受整数)。
另一个选择(取决于您需要知道某个东西是否为数字的原因)是假设它是数字,如果不是,则在需要数字的任何代码位处将引发错误。
我并不是说这些方法总是更好(不像有些人...),只是值得考虑。
thing
是布尔值,thing + 1
不会引发TypeError。另外,如果thing
是numpy.nan
,那么该函数将返回True,这可能没有意义。 - Toby Pettything +1
将起作用,并且不会引发任何异常,我怀疑这不是期望的行为,如果想要捕获是否为类似列表的“var”。 - lurix66
numpy.isfinite
应该可以解决问题。 - Jon Warneke