
On 4/23/2021 5:22 AM, Stephen J. Turnbull wrote:
But we could change int.__format__ to allow 's' as a format code[1], automagically calling str(), just as 'efg' are allowed and automagically call float(). ... [1] I recognize this may imply many changes, since some (all?) of the builtin types seem to be supported by object.__format__.
object.__format__ doesn't do anything except call str() (assuming the format spec is not specified). It's each builtin type that supports formatting itself. There's an int.__format__, str.__format__, float.__format__, datetime.datetime.__format__, decimal.__format__, etc. Many of these use some shared code to do the heavy lifting, and many of them share the same format spec, or much of it. But conceptually they're all distinct and each one can interpret the format spec however they choose. For example, int.__format__ chose not to support the "s" type. And datetime.datetime.__format__ chose to call .strftime(). It's unfortunate that PEP 3101 says "If an object does not define its own format specifiers, a standard set of format specifiers is used", because it's not true. It should say that "Many types will chose to support a standard format specifier". Eric