Suggested coding style

rantingrick rantingrick at gmail.com
Fri Sep 30 16:19:20 CEST 2011


On Sep 29, 11:49 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> Nope, that doesn't work.
>
> >>> "{0:0>10}".format("-1234")
>
> '00000-1234'
>
> The whole point of zfill is that it handles signs correctly.

py> "{0:-010d}".format(-1234)
'-000001234'

My point was: Use the {char}{repeat}d format for integers and the
{char}{<|>|=}{repeat} for strings. Problem solved. No more need for
zfill.

py> "{0:0>10}".format(-1234)
'00000-1234'

What result would you expect from that string argument? I think it
behaves as anyone would expect. If you have a str and you want it
interpreted as a negative integer then cast it.

py> "{0:010d}".format(int("-1234"))
'-000001234'

If you would like for the spec to handle the case of integers and
strings transparently then you need to lobby to have the API changed.
Maybe they could add a !i like the !s and !r which would be explicit.
However, i don't think implicit coercion of strings to integers is a
good idea. Using the int function or !i removes and ambiguities.

For me, the spec works just fine as is.



More information about the Python-list mailing list