[Python-3000] More PEP 3101 changes incoming
Greg Ewing
greg.ewing at canterbury.ac.nz
Fri Aug 10 09:21:04 CEST 2007
Eric V. Smith wrote:
> If you want:
>
> x = 3
> "{0:f}".format(x)
>
> then be explicit and write:
>
> "{0:f}".format(float(x))
That would be quite inconvenient, I think. It's very common
to use ints and floats interchangeably in contexts which
are conceptually float. The rest of the language facilitates
this, and it would be a nuisance if it didn't extend to
formatting.
So I think that ints and floats should know a little about
each other's format specs, just enough to know when to
delegate to each other.
> 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 can be done.
> 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 inclined to think the best thing is just to declare
that 'r' is special and gets intercepted by format().
It means that __format__ methods don't *quite* get
complete control, but I think it would be a practical
solution.
Another way would be to ditch 'r' completely and just
tell people to wrap repr() around their arguments if
they want it. That might be seen as a backward step
in terms of convenience, though.
--
Greg
More information about the Python-3000
mailing list