[Python-3000] More PEP 3101 changes incoming
Eric V. Smith
eric+python-dev at trueblade.com
Fri Aug 10 03:26:30 CEST 2007
I'm just getting back from vacation and trying to catch up. I think
I've caught the sense of the discussion, but forgive me if I haven't.
Talin wrote:
> The reason is that for some types, the __format__ method can define its
> own interpretation of the format string which may include the letters
> 'rtgd' as part of its regular syntax. Basically, he wants no constraints
> on what __format__ is allowed to do.
Why would this not be true for all types? Why have int's interpret "f",
or other things that don't apply to int's?
If you want:
x = 3
"{0:f}".format(x)
then be explicit and write:
"{0:f}".format(float(x))
I realize it's a little more verbose, but now the __format__ functions
only need to worry about what applies to their own type, and we get out
of the business of deciding how and when to convert between ints and
floats and decimals and whatever other types are involved.
And once you decide that the entire specifier is interpreted only by the
type, you no longer need the default specifier (":f" in this case), and
you could just write:
"{0}".format(float(x))
That is, since we already know the type, we don't need to specify the
type in the specifier. Now the "d", or "x", or whatever could just be
used by int's (for example), and only as needed.
I grant that repr() might be a different case, as Greg Ewing points out
in a subsequent message. But maybe we use something other than a colon
to get __repr__ called, like:
"{`0`}".format(float(x))
I'm kidding with the back-ticks, of course. Find some syntax which can
be disambiguated from all specifiers. Maybe:
"{0#}".format(float(x))
Or something similar.
Eric.
More information about the Python-3000
mailing list