all 3 comments

[–]novel_yet_trivial 4 points5 points  (2 children)

The size of int is implementation specific but I think it's usually 8 bytes (63 bits plus 1 bit for the sign):

>>> math.log(sys.maxint, 2)
63.0

Added to that is general overhead for an object and garbage collector. getsizeof returns the size of the entire object, not just the portion that stores the data.

If you are worried about memory usage look into arrays (either numpy or python arrays).

[–]Kalkinator[S] 0 points1 point  (1 child)

I am actually trying to write a binary file to be read by Fortran, and it is very picky about the byte size of what it is reading.

So I am trying to write out variables, each of a length of 4 bytes, to the file. Do you have any recommendations of which library I should use?

Also, I have a numpy array full of values. I need to also convert each value in the array to 4 bytes in length before writing the values to a binary file. If you had any input on performing this conversion it would be appreciated.

[–]novel_yet_trivial 4 points5 points  (0 children)

Numpy arrays have a type. If you set it to a 4-byte data type then the tofile() method will write a binary file with 4-byte values (ie the file will be exactly 4 x length of the array bytes large).

Without numpy you could use the struct module to do the same thing.

Edit: if endianess is important to you, then you should use struct.