Numeric coercions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 7 06:48:20 CEST 2013


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 :-)


-- 
Steven



More information about the Python-list mailing list