Does Python allow access to some of the implementation details?

Tim Peters tim.peters at
Sat Jan 7 05:34:15 CET 2006

[Claudio Grondi]
> Let's consider a test source code given at the very end of this posting.
> The question is if Python allows somehow access to the bytes of the
> representation of a long integer or integer in computers memory?

CPython does not expose its internal representation of longs at the
Python level.

> Or does Python hide such implementation details that deep, that there is
> no way to get down to them?

As above.

> The test code below shows, that extracting bits from an integer value n
> is faster when using n&0x01 than when using n%2 and I suppose it is
> because %2 tries to handle the entire integer,

It not only tries, it succeeds ;-)

>where &0x01 processes only the last two bytes of it

If x and y are positive longs, the time required to compute x&y in all
recent CPythons is essentially proportional to the number of bits in
min(x, y).

> ...
> 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?


> As I am on Python 2.4.2 and Microsoft Windows, I am interested in
> details related to this Python version (to limit the scope of the
> question).

Doesn't really matter:  same answers for all recent versions of
CPython on all platforms.  If you go back far enough, in older
versions of CPython the time to compute x&y was proportional to the
number of bits in max(x, y) (instead of min(x, y)).

More information about the Python-list mailing list