Jul-09-2018, 10:18 PM
@Windspar, that doesn't work for Decimal, imaginary, long, or other kinds of numbers.
@Skaperen, what exactly do you mean by "determine if an object passed in to a function is a number"? It sounds like you already have a Python object, so you're not dealing with a user-input string.
Python is not only dynamically typed (by design) but also duck typed (by convention), so yes as you say the "Pythonic" thing to do would be to try to use it and then catch an exception if you're really concerned about an operation not being supported, but in practice we usually don't bother looking for an exception. Usually we wouldn't have an is_number function like you're describing, even.
@Skaperen, what exactly do you mean by "determine if an object passed in to a function is a number"? It sounds like you already have a Python object, so you're not dealing with a user-input string.
Python is not only dynamically typed (by design) but also duck typed (by convention), so yes as you say the "Pythonic" thing to do would be to try to use it and then catch an exception if you're really concerned about an operation not being supported, but in practice we usually don't bother looking for an exception. Usually we wouldn't have an is_number function like you're describing, even.