Numeric coercions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 7 05:56:26 CEST 2013


I sometimes find myself needing to promote[1] arbitrary numbers 
(Decimals, Fractions, ints) to floats. E.g. I might say:

numbers = [float(num) for num in numbers]

or if you prefer:

numbers = map(float, numbers)

The problem with this is that if a string somehow gets into the original 
numbers, it will silently be converted to a float when I actually want a 
TypeError. So I want something like this:

def promote(x):
    if isinstance(x, str): raise TypeError
    return float(x)

but I don't like the idea of calling isinstance on every value. Is there 
a better way to do this? E.g. some operation which is guaranteed to 
promote any numeric type to float, but not strings?

For the record, calling promote() as above is about 7 times slower than 
calling float in Python 3.3.




[1] Or should that be demote?

-- 
Steven



More information about the Python-list mailing list