Re: Numeric coercions

2013-07-07 Thread Joshua Landau
On 7 July 2013 09:15, Vlastimil Brom wrote: > 2013/7/7 Steven D'Aprano : >> 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,

Re: Numeric coercions

2013-07-07 Thread Vlastimil Brom
2013/7/7 Steven D'Aprano : > 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

Re: Numeric coercions

2013-07-06 Thread Joshua Landau
On 7 July 2013 06:14, Joshua Landau wrote: > On 7 July 2013 05:48, Steven D'Aprano > wrote: >> On Sun, 07 Jul 2013 05:17:01 +0100, Joshua Landau wrote: >> >>> On 7 July 2013 04:56, Steven D'Aprano >>> wrote: >> ... def promote(x): if isinstance(x, str): raise TypeError return float

Re: Numeric coercions

2013-07-06 Thread Joshua Landau
On 7 July 2013 05:48, Steven D'Aprano wrote: > On Sun, 07 Jul 2013 05:17:01 +0100, Joshua Landau wrote: > >> On 7 July 2013 04:56, Steven D'Aprano >> wrote: > ... >>> def promote(x): >>> if isinstance(x, str): raise TypeError return float(x) > > from operator import methodcaller > saf

Re: Numeric coercions

2013-07-06 Thread Steven D'Aprano
On Sun, 07 Jul 2013 05:17:01 +0100, Joshua Landau wrote: > On 7 July 2013 04:56, Steven D'Aprano > wrote: ... >> def promote(x): >> if isinstance(x, str): raise TypeError return float(x) from operator import methodcaller safe_float = methodcaller("__float__") Nice! That's almost

Re: Numeric coercions

2013-07-06 Thread Joshua Landau
On 7 July 2013 04:56, Steven D'Aprano wrote: > 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

Numeric coercions

2013-07-06 Thread Steven D'Aprano
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,