Behaviour change of object().format()
Hi, In 3.3 I could do the following
"{x:s}".format(**{'x': [1, 2, 3]}) '[1, 2, 3]'
But in 3.4
"{x:s}".format(**{'x': [1, 2, 3]}) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: non-empty format string passed to object.__format__
Is this intentional? regards, James Swift
On 05/06/2014 10:33 AM, James Swift wrote:
Hi,
In 3.3 I could do the following
"{x:s}".format(**{'x': [1, 2, 3]}) '[1, 2, 3]'
But in 3.4
"{x:s}".format(**{'x': [1, 2, 3]}) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: non-empty format string passed to object.__format__
Is this intentional?
Yes. See http://bugs.python.org/issue7994 for the rationale. You can use:
"{x!s:15s}".format(**{'x': [1, 2, 3]}) '[1, 2, 3] '
That is, convert it first to a string (with !s) then use whatever string formatting specifier you want (here, '15s'). This should work under all 3.x versions (I think, haven't checked). BTW, for this specific case, you might want to use format_map:
"{x!s:15s}".format_map({'x': [1, 2, 3]}) '[1, 2, 3] '
Eric.
participants (2)
-
Eric V. Smith
-
James Swift