hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ??

Bengt Richter bokr at oz.net
Sun Aug 17 15:52:54 EDT 2003


>>> hex(-5)
__main__:1: FutureWarning: hex()/oct() of negative int will return a signed string in Python 2.4
 and up
'0xfffffffb'
>>> hex(-5)
'0xfffffffb'
>>> hex(-5L)
'-0x5L'

That is sooo ugly. I suppose it is for a backwards compatible repr, but couldn't we
at least have hex(n, newformat=False) so that we can do

    hex(-5, True) => 1xb   # 1x signals an arbitrary number of prefixed f's
    hex( 5, True) => 0x5

and have int() and long() recognize these?

Also would need a variant of %x and %X for formatting with the % operator.

Regards,
Bengt Richter




More information about the Python-list mailing list