[Python-ideas] One way to do format and print

Random832 random832 at fastmail.com
Tue Sep 8 03:46:53 CEST 2015


Nick Coghlan <ncoghlan at gmail.com> writes:
> The final reason for introducing a distinct formatting system doesn't
> relate to syntax, but rather to semantics. Mod-formatting is defined
> around the builtin types, with "__str__" as the catch-all fallback for
> interpolating arbitrary objects. PEP 3101 introduced a new *protocol*
> method (__format__) that allowed classes more control over how their
> instances were formatted, with the typical example being to allow
> dates and times to accept strftime formatting strings directly rather
> than having to make a separate strftime call prior to formatting.
> Python generally follows a philosophy of "constructs with different
> semantics should use different syntax"

I guess my problem is that I don't consider the fact that %s forces
something to string, %f to float, etc, to be desired semantics, I
consider it to be a bug that could, and *should*, have been changed by
an alternate-universe PEP.

There's nothing *good* about the fact that '%.20f' % Decimal('0.1')
gives 0.10000000000000000555 instead of 0.10000000000000000000, and that
there are no hooks for Decimal to make it do otherwise. There's nothing
that would IMO be legitimately broken by allowing it to do so. You
could, for example, have object.__format__ fall back on the type
conversion semantics, so that it would continue to work with existing
types that do not define their own __format__.



More information about the Python-ideas mailing list