[Python-3000] More PEP 3101 changes incoming
Nicko van Someren
nicko at nicko.org
Thu Aug 2 18:47:45 CEST 2007
On 2 Aug 2007, at 03:01, Talin wrote:
> In general, you can think of the difference between format
> specifier and
> alignment specifier as:
>
> Format Specifier: Controls how the value is converted to a
> string.
> Alignment Specifier: Controls how the string is placed on the
> line.
>
> Another change in the behavior is that the __format__ special
> method can
> only be used to override the format specifier - it can't be used to
> override the alignment specifier. The reason is simple: __format__ is
> used to control how your object is string-ified. It shouldn't get
> involved in things like left/right alignment or field width, which are
> really properties of the field, not the object being printed.
Say I format numbers in an accounting system and, in the absence of
being able to colour my losses in red, I choose the parentheses sign
representation style (). In this case I'd like to be able to have my
numbers align thus:
1000
200
(3000)
40
(50000)
I.e. with the bulk of the padding applied before the number but
conditional padding after the number if there is no closing bracket.
If the placement is done entirely outside the __format__ method then
you to make sure that it is documented that, when using the () style
of sign indicator, positive numbers need to have a space placed
either side, e.g. -100 goes to "(100)" but +100 does to " 100 ". If
you do this then it should all come out in the wash, but I think it
deserves a note somewhere.
Cheers,
Nicko
More information about the Python-3000
mailing list