I found some old Python code that was doing something like:
if type(var) is type(1):
...
As expected, pep8 complains about this recommending usage of isinstance().
Now, the problem is that the numbers module was added in Python 2.6 and I need to write code that works with Python 2.5+
So if isinstance(var, Numbers.number) is not a solution.
Which would be the proper solution in this case?
To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not. After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”.
To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.
In Python 2, you can use the types module:
>>> import types
>>> var = 1
>>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
>>> isinstance(var, NumberTypes)
True
Note the use of a tuple to test against multiple types.
Under the hood, IntType is just an alias for int, etc.:
>>> isinstance(var, (int, long, float, complex))
True
The complex type requires that your python was compiled with support for complex numbers; if you want to guard for this use a try/except block:
>>> 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)
...
or if you just use the types directly:
>>> try:
... NumberTypes = (int, long, float, complex)
... except NameError:
... # No support for complex numbers compiled
... NumberTypes = (int, long, float)
...
In Python 3 types no longer has any standard type aliases, complex is always enabled and there is no longer a long vs int difference, so in Python 3 always use:
NumberTypes = (int, float, complex)
Last but not least, you can use the numbers.Numbers abstract base type (new in Python 2.6) to also support custom numeric types that don't derive directly from the above types:
>>> import numbers
>>> isinstance(var, numbers.Number)
True
This check also returns True for decimal.Decimal() and fractions.Fraction() objects.
This module does make the assumption that the complex type is enabled; you'll get an import error if it is not.
Python 2 supports four types for numbers int,float, long and complexand python 3.x supports 3:int, float and 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With