[Python-Dev] Extending hex() and oct() to accept strings

Guido van Rossum guido at python.org
Wed Aug 27 21:52:05 EDT 2003


> To illustrate:
> 
> >>> hex('ABC')
> "'\\x41\\x42\\x43'"
> >>> print hex('ABC')  # This is what I was trying to do today
> '\x41\x42\x43'
> >>> eval(hex('ABC'))
> 'ABC'
> 
> >>> oct('ABC')
> "'\\101\\102\\103'"
> >>> print oct('ABC')
> '\101\102\103'
> >>> eval(oct('ABC'))
> 'ABC'
> 
> Are there any subtle issues with supporting this that I'm not seeing?

This is indeed elegant because it preserves the property that hex()
and oct() have for numbers: eval(hex(x)) == x == eval(oct(x)).

However, I'm not sure if it should be added.  It adds yet another
feature to document and support (think of Jython, Pychecker, etc.),
and I think that the number of people who care about hexadecimal
strings is becoming a vanishingly small fraction of the total number
of programmers.  (Nobody has cared about octal for a long time; the
only use for it at this point is to show unix file permission bits.)

And I think that what you really wanted is probably closer to this:

  >>> import binascii
  >>> binascii.hexlify('ABC')
  '414243'
  >>> 

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list