Numeric coercions

Vlastimil Brom vlastimil.brom at gmail.com
Sun Jul 7 10:15:27 CEST 2013


2013/7/7 Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> 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:
>
>...
> --
> Steven
> --
Hi,
I guess, a naive approach like

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

wouldn't satisfy the performance requirements, right?

vbr



More information about the Python-list mailing list