Couple of simple questions

Greg Jorgensen gregj at pobox.com
Sat Mar 31 21:17:42 EST 2001


"nomad" <nomad***@***freemail.absa.co.za> wrote:

> Does anybody know what size python variabel types are?
> Eg. how many bytes is and integer?  What about a long?  I have
> searched through the docs but I cant find a reference to the byte
> sizes.  The reason is that I'm battling with struct.py and some
> complex C++ structs.  Any help here would greatly appreciated

The range of values for integers, long integers, floats, and doubles are
architecture-dependent. From the Python Reference Manual section 2.4.4:

    Plain integer decimal literals must be at most 2147483647 (i.e., the
largest
    positive integer, using 32-bit arithmetic). Plain octal and hexadecimal
literals
    may be as large as 4294967295, but values larger than 2147483647 are
    converted to a negative value by subtracting 4294967296. There is no
limit
    for long integer literals apart from what can be stored in available
memory.

So depending on the CPU and OS, an integer may occupy 1, 2, 3, or 4 bytes.
On a 32-bit OS a Python integer is equivalent to a long int in C/C++.

C and C++ don't have a built-in type analogous to Python's long integers.

You are on the right track using the struct module to map C++ structs.
Besides the relatively simple mapping of variables based on size, you may
have to deal with big-endian/little-endian bit ordering, and possibly struct
padding/alignment. You should refer to your C++ compiler documentation to
find out how it aligns struct objects.

--
Greg Jorgensen / programmer, pedant, raconteur / Portland, Oregon, USA





More information about the Python-list mailing list