Does Python allow access to some of the implementation details?

Paul Rubin http
Fri Jan 6 21:24:18 EST 2006


Claudio Grondi <claudio.grondi at freenet.de> writes:
> The question is if Python allows somehow access to the bytes of the
> representation of a long integer or integer in computers memory?

No it doesn't, and that's a good thing, since the internal
representation is a little bit surprising (it stores 15 bits of the
long int in each 16-bit word of the representation, to make the
arithmetic routines  a little simpler).

> If it were possible to 'tell' the %2 operation to operate
> only on one short of the integer number representation there will be
> probably no difference in speed. Is there a way to do this efficiently
> in Python like it is possible in C when using pointers and recasting?

The usual trick to get at the contents of a long is to use hex
conversion and maybe the array module (untested):

   a = '%x' % n
   if len(a) % 2 == 1: 
      a = '0' + a
   s = array.array('B', binascii.unhexlify(a))

s now gives you the individual bytes of n.



More information about the Python-list mailing list