On Jan 16, 2006, at 8:18 PM, Barry Warsaw wrote:
On Tue, 2006-01-17 at 15:08 +1100, Andrew Bennetts wrote:
My reaction having read this far was "huh?". It took some time (several seconds) before it occurred to me what you wanted str(5,2) to mean, and why it should give '101'.
If you'd proposed, say (5).as_binary() == '101', or "5".encode ("base2"), I wouldn't have been as baffled. Or perhaps even str(5, base=2), but frankly the idea of the string type doing numeric base conversions seems weird to me, rather than symmetric.
I wouldn't mind seeing arbitrary base encoding of integers included somewhere, but as a method of str -- let alone the constructor! -- it feels quite wrong.
Hear, hear. I was similarly perplexed when I first read that!
The only bases I've ever really had a good use for are 2, 8, 10, and 16. There are currently formatting codes for 8 (o), 10 (d, u), and 16 (x, X). Why not just add a string format code for unsigned binary? The obvious choice is probably "b". For example:
'%08b' % (12) '00001100' '%b' % (12) '1100'
I'd probably expect "5".encode("base2") to return '00110101', because "5".encode("hex") returns '35' -bob