2012-06-26 16 views
32

我發現了一些舊的Python代碼這是做這樣的事情:如何正確使用python的isinstance()來檢查一個變量是否是一個數字?

if type(var) is type(1): 
    ... 

正如預期的那樣,pep8抱怨的isinstance()此推薦使用。現在

,問題是,numbers模塊在Python 2.6中加入,我需要編寫使用Python 2.5+

工作代碼,所以if isinstance(var, Numbers.number)是不是一個解決方案。

這將是在這種情況下適當的解決方案?

+0

如果你願意使用numpy,'numpy.isfinite'應該可以做到。 –

回答

83

可以使用types module

>>> 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類型要求你的蟒蛇與複雜的數字支持編譯;如果你想守護這個使用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) 
... 

最後但並非最不重要的,你可以使用numbers.Numbers abstract base type(新中Python 2.6中),也支持自定義數值類型不直接從上述類型推導:

>>> import numbers 
>>> isinstance(var, numbers.Number) 
True 

這個模塊確實讓該complex類型啓用的假設;如果不是,你會得到一個導入錯誤。

+3

'isinstance()'可以取對象的元組嗎?!?哇,老兄... –

+0

@Def_Os:是的,這是自Python 2.2以來的一項功能。 –

+0

你真的應該把你的答案放在最重要的位置。 –

13

的Python 2支持四種類型的數字intfloatlongcomplexpython 3.x支持3:intfloatcomplex

>>> 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 
2

根據您在使用duck typing這這可能是一個更好的方法(這是certainlycommonlyrecommended)。 Martijn Pieters的做法存在的問題是,您將永遠錯過列表中的某些類型的號碼。關於我的頭頂,您的代碼將無法使用:sympy有理數,任意精度整數和複數的任何實現。

一種替代方法是編寫一個函數:

def is_number(thing): 
    try: 
     thing + 1 
     return True 
    except TypeError: 
     return False 

此代碼應與任何合理地實施了許多的工作。當然還有一個主要的缺點:它也可以用不合理的方式實現大量非數字(即,如果加運算符被重載並接受整數)。

另一種選擇(取決於你爲什麼需要知道某物是否是數字)是假設它是一個數字,如果不是這樣,錯誤會被代碼中需要的數字拋出。

我不是說這些方法總是比較好(不像有些人......),只是他們值得考慮。

相關問題