[Python-ideas] %-formatting with Decimals

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Mar 11 23:49:23 CET 2014


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

There's also no mention of this in the decimal docs.


Oscar


More information about the Python-ideas mailing list