get a list printed in hexadecimal notation
Peter Hansen
peter at engcorp.com
Wed Jul 17 08:43:53 EDT 2002
Oliver Eichler wrote:
>
> Is it possible to print a mixed list with strings and numbers and having the
> numbers printed as hexadecimal notation?
>
> something like:
>
> >>>l = ('spam',10,15,'more spam')
> >>>print l
> ('spam',0xA,0xF,'more spam')
>
> or similar...
>
> The only way I could think of is to write my own print function. Is there an
> easy way?
Maybe, if you don't mind copying the list, some hack like this?
(Using Python 2.2... not sure it works in earlier versions.)
>>> class HexRepInt(int):
... def __repr__(self):
... return self.__hex__()
...
>>> l = ['spam', 10, 15, 'more spam']
>>> m = [type(x) == type(0) and HexRepInt(x) or x for x in l]
>>> print m
['spam', 0xa, 0xf, 'more spam']
-Peter
More information about the Python-list
mailing list