[Python-3000] PEP 3101 str.format() equivalent of '%#o/x/X'?

Nick Coghlan ncoghlan at gmail.com
Fri May 30 01:10:23 CEST 2008


Eric Smith wrote:
> Marcin ‘Qrczak’ Kowalczyk wrote:
>> 2008/5/29 Eric Smith <eric+python-dev at trueblade.com>:
>>
>>> I don't see it as a big problem.  You can now use any prefix you want,
>>> instead of the hard coded values that # supplied.
>>
>> Except that it works incorrectly for negative numbers.
> 
> Excellent point.  If only this had been brought up back when the PEP was 
> written :(
> 
> Any suggestions on how to improve the situation?  I guess we could add 
> '#' back in to the format specifier.  I can't really think of any other 
> way that doesn't involve converting the number to a string and then 
> operating on that, just to get the sign.
> 
> I'm reasonably sure I could implement that before the beta (next 
> Wednesday) if a decision is reached before this weekend.

Doing the right thing for negative numbers is a good point. It also 
means the prefix can be handled properly when dealing with aligned 
fields. The following update to the standard format specifier in the PEP:

   [[fill]align][#][sign][0][minimumwidth][.precision][type]

   The '#' prefix option inserts the appropriate prefix characters 
('0b', '0o', '0x', '0X') when displaying numbers in binary, octal or 
hexadecimal formats. The prefix is inserted into the displayed number 
after the sign character and fill characters (if any), but before any 
leading zeroes.

Cheers,
Nick.
-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list