|
Re: The size of a data type
|
Posted: Sep 1, 2004 1:11 AM
|
|
> However, I don't agree with the following: > > > Python doesn't have statically-typed variables like C, > > so there's no analog to C's sizeof(). > > There is no reason we shouldn't be able to determine the > size, in bytes, of an object in Python. In C, sizeof() is > a compile-time expression (you can even use it in an array > dimension declaration, so it's obviously constant). In a > dynamic language like Python, it should be run-time, but I > see no reason why it couldn't be done.
I didn't say there was no need for it; I said there is no analog to it. But I don't think there's any need for sizeof() in Python either. In C, sizeof is used (a) to make code portable across hardware platforms, and (b) for pointer arithmetic. Neither of those purposes have analogs in Python code.
The example you gave, reading binary data from a file into a C struct, seems to be a third use, but it's actually just an example of pointer arithmetic.
Obviously someone could implement a runtime sizeof() function that returned the number of bytes occupied by an object at that time, but I don't think it would be useful in Python. You can't use that information in the way C uses it. In the context of C sizeof() is a necessary construct; in the context of Python it's at best useless and at worst misleading.
All that said, you can find some answers in the O'Reilly book "Python In A Nutshell." Page 34:
"A plain integer takes up a few bytes of memory and has minimum and maximum values that are dictated by machine architecture. sys.maxint is the largest plain integer, while -sys.maxint-2 is the largest negative one. On typical 32-bit machines, sys.maxint is 2147483647."
The Python long integer type occupies at least as much memory necessary to store the value. Python automatically promotes plain integers to long integers, but not from long to plain. Long integers would need at least enough log2(n)+1 bits, most likely padded out to the next 32-bit boundary. But what use is this information? How would you use it?
According to the same book, "A Python floating-point value corresponds to a C double and share its limits of range and precision. ... Python currently offers no way to find out this range and precision."
Strings would have to occupy at least one byte per character, but since Python strings can be Unicode rather than ASCII, even predicting the number of bytes occupied by a string of length N gets tricky.
C and Python actually have few data types in common: one size of integer and one size of floating-point.
As another poster pointed out, there are Python libraries that can deal with mapping structures to binary data and files. And if you need to write binary data Python can easily do that, too. But Python works at a higher level of abstraction than C, at a level where mapping structures to files byte-by-byte, worrying about alignment and endian-ness, etc. are details intentionally hidden from the Python programmer. If you need to operate at that level -- just above the raw machine language -- C is your best choice.
Greg Jorgensen PDXperts LLC - Portland, Oregon, USA
|
|