[Python-Dev] str with base

Neal Norwitz nnorwitz at gmail.com
Wed Jan 18 07:08:06 CET 2006


On 1/17/06, Guido van Rossum <guido at python.org> wrote:
>
> > The difference between hex() and oct() and the proposed binary() is
>
> I'd propose bin() to stay in line with the short abbreviated names.

Are these features used enough to have 3 builtins?
Would format(number, base) suffice?

  format(5, base=2) == '101'
  format(5, base=8) == '5'
  format(5, base=8, prefix=True) == '05'
  format(5, base=16) == '5'
  format(5, base=16, prefix=True) == '0x5'

Or something like that.  Then there can be symmetry with int()
(arbitrary bases) and we get rid of 2 other builtins eventually.  Not
sure if there are/should be uses other than number formating.

> > that hex() and oct() return valid Python expressions in that base.
> > In order for it to make sense, Python would need to grow some syntax.
>
> Fair enough. So let's define it.
>
> > If Python were to have syntax for binary literals, I'd propose a
> > trailing b: "1100b".  It would be convenient at times to represent
> > bit flags, but I'm not sure it's worth the syntax change.
>
> Typically, suffixes are used to indicated *types*: 12L, 12j, and even
> 12e0 in some sense.
>
> The binary type should have a 0b prefix.

-0.  Is this common enough to add (even in 3k)?  For the instances I
could have used this, it would have been completely impractical since
the hex strings were generally over 80 characters.

n


More information about the Python-Dev mailing list