Numbers and numbers in Python
Did you know that Python has three base number types?
- integers
- floats
- complex numbers
cmath, but that’s a topic for another time.
You can actually access quite a lot of additional types of number from commonly used packages, but particularly from numpy specifically, which provides things like numpy.int64: a signed, 64-bit integer. Types like this are commonly built-in in older languages more oriented toward number crunching, with FORTRAN perhaps being the most obvious example (if ever there was a language made for working with numbers it was FORTRAN). However, I assume Python eschewed them in favour of accessibility. Python is about being accessible and readable, so there are just 3 base numerical types and one of those is fairly uncommon (complex numbers).
OK, great, but when are you going to talk about numbers?
Well, the important part of the preamble is to establish that Python does have a lot of numerical types, despite appearances. Many of those are available through numpy, but this is Ansys, the engineering software company. Numerical computation is what we do. So we often end up working with them, a lot. But Python is also a dynamically typed language and one that is particularly relaxed when it comes to numbers.
NaNs are tricky beasts to work with and I won’t dwell on them here, but you should check out this post by Julia Di Rosso if you want to know more.And at some point as a developer you will hit this problem. “How do I check if my variable is a number?” That’s where
numbers comes in. This is a builtin-library of numeric abstract base classes (or “ABCs”).
If you know what that means, great! If you don’t then the quick summary is that its a collection of classes that are not intended to ever be instantiated. Instead, they are to be inherited from, with built-in methods that can be overwritten or made use of in other ways. Python has a built-in library for building your own or otherwise working with them called abc
In particular numbers has a variety of numeric ABCs but the most general one, is Number. This is how you solve the posed problem. We can use it in conjunction with isinstance to check if anything we pass to it is a numeric value!
isinstance(a, b)is a simple built-in that checks if the first provided argumentais an instantiated instance of the secondb. Generally, it should be avoided as it is considered a code “smell”.
Code “smells” are features of a program that suggest an underlying issue, but aren’t necessarily bad in and of themselves.isinstanceis a good example because the function isn’t bad but it is commonly misused, so seeing it appear in a program could be a sign that something elsewhere is wrong. If you find yourself using it a lot you should take a closer look at your code and work out why!
Conclusion
As explained above, you should avoid usingisinstance in long programs generally. However, when debugging or scripting, which we, as developers, probably do more than anything else, this is an invaluable tool to have at your disposal. It’s another tool for the box.