faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 16 people (63%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I find the byte length of a float, double or an int?
Are all standard python float, double, ints, etc of a fixed format?

Nov 11th, 2000 10:33
Dave Brueck, unknown unknown, Tim Peters


You can't find the byte length short of reading the source code.

A Python float is a C double.
Python doesn't have a double type.
A Python int is a C long.

Every Python object also comes with some amount of object hair, like a
refcount and a pointer to its type object ... again, you have to read 
the source to get the full story.

*But*, if you're interested in byte length because you want to send the 
data as a network message or if you want to store data in a file or 
array and calculate an offset into that array, you can use the struct 
module, which lets you pack data into a string of bytes. You can 
optionally tell it to put the bytes in network order so that it will 
work on any platform, and you can also use the struct.calcsize method 
to see how much space the packed version will use.

There is also the array module for storing homogeneous data, and it has 
an itemsize method, but the value returned can vary by platform.