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?
Python 2 supports four types for numbers
int
,float
,long
andcomplex
andpython 3.x
supports 3:int
,float
andcomplex
In Python 2, you can use the
types
module:Note the use of a tuple to test against multiple types.
Under the hood,
IntType
is just an alias forint
, etc.: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:or if you just use the types directly:
In Python 3
types
no longer has any standard type aliases,complex
is always enabled and there is no longer along
vsint
difference, so in Python 3 always use: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:This check also returns
True
fordecimal.Decimal()
andfractions.Fraction()
objects.This module does make the assumption that the
complex
type is enabled; you'll get an import error if it is not.Depending on what you're using this in duck typing could be a better approach (it's certainly commonly recommended). The problem with Martijn Pieters' approach is that you will always miss some types of number from your list. Off the top of my head your code won't work with: sympy rational numbers, arbitrary precision integers and any implementation of complex numbers.
One alternative is to write a function like this:
This code should work with any reasonable implementation of a number. Of course there is a major downside: it will also work with an unreasonable implementation of plenty of non-numbers (i.e. if the plus operator is overloaded and accepts an integer).
Another alternative (depending on why you need to know if something is a number) is to just assume it is a number, and if it isn't errors will be thrown by whichever bit of the code requires a number.
I'm not saying these approaches are always better (unlike some people...) just that they are worth considering.