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,
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
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
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
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
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
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,