[Python-Dev] Re: [Python-checkins] python/dist/src/Objects stringobject.c,2.171,2.172

Tim Peters tim.one@comcast.net
Sun, 28 Jul 2002 22:55:45 -0400


[Neal Norwitz]
> The intent was to convert an int/long to a double in the case of
> '%g' et al and from a double to an int in the case of '%d'.

[Greg Ewing]
> Are you sure the latter part of that is a good idea?  As a general
> principle, I don't think float->int conversions should be done
> automatically. What is the Python philosophy on that?

The philosophy for format codes is looser than elsewhere, else, e.g.,

    "%s" % object

would raise TypeError whenever object was a number or list, etc.  I've often
used %d with floats when I want them rounded to int and don't want to bother
remembering how to trick a float format into suppressing the decimal point.
Unfortunately, that's not quite what %d does (it truncates).  Whatever, %s
is like invoking str(), %r like invoking repr(), %d like invoking long(),
and %g/e/f like invoking float() (although these are variants of long() and
float() that refuse string arguments -- that's the exception that makes the
rule easy to remember <wink>).