Numeric coercions

Joshua Landau joshua.landau.ws at gmail.com
Sun Jul 7 01:30:16 EDT 2013


On 7 July 2013 06:14, Joshua Landau <joshua.landau.ws at gmail.com> wrote:
> On 7 July 2013 05:48, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Sun, 07 Jul 2013 05:17:01 +0100, Joshua Landau wrote:
>>
>>> On 7 July 2013 04:56, Steven D'Aprano
>>> <steve+comp.lang.python at pearwood.info> 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 as fast as calling float. That will probably do :-)
>
> *Almost* as fast?
>
*incorrect timings*
>
> Actually, it's a fair bit faster (yes, I am purposely not showing you
> the results for Decimals).

*sigh*, it seems that was just because it's slower to access
__builtins__ than globals(). float() is maybe 2% faster if you fix
that.



More information about the Python-list mailing list