[Python-ideas] %-formatting with Decimals
Ethan Furman
ethan at stoneleaf.us
Tue Mar 11 23:46:45 CET 2014
On 03/11/2014 03:49 PM, Oscar Benjamin wrote:
> On 11 March 2014 21:58, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 03/11/2014 02:51 PM, random832 at fastmail.us wrote:
>>>
>>> The bug to which I was referring involves not calling __format__ on
>>> whatever object is passed in as str.format does - Decimal not being
>>> float is no impediment to str.format and Decimal.__format__ accepting
>>> the same format strings that float.__format__ does.
>>
>> That is not a bug. str.__mod__ is not documented as calling an object's
>> __format__ method, and indeed it does not.
>
> Instead it calls float on the object:
>
>>>> class A:
> ... def __float__(self):
> ... print('__float__ called')
> ...
>>>> '%f' % A()
> __float__ called
Well, you did ask for a float, and how else are you going to get one? ;)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: nb_float should return float object
>
> There's no mention of that anywhere in the docs though:
> http://docs.python.org/3.4/library/stdtypes.html#printf-style-string-formatting
We should probably fix that.
--
~Ethan~
More information about the Python-ideas
mailing list