1

Possible Duplicate:
How do I determine the size of an object in Python?

In C if I do

USHORT x;
ULONG y;
ULONGLONG yy;

printf("%d %d %d\n", sizeof(x), sizeof(y), sizeof(yy))

Output: 2 4 8

How can I get this in Python if I were to to

valByte = 0x16
valWord = 0x1234
valDWord = 0x12345678
val64Word = 0x1234567887654321

Output I expect should be 1, 2, 4, 8

thanks

8
  • 1
    Python doesn't really work like that - what exactly are you trying to do? Commented Dec 28, 2012 at 23:35
  • 1
    Python integers do not have a size. What are you trying to achieve? Commented Dec 28, 2012 at 23:40
  • @MartijnPieters sure they do, it just varies. You can use sys.getsizeof, though the value it gives should probably not be used for anything. Commented Dec 28, 2012 at 23:42
  • @kwatford: have you actually tried to use that function on integers? Commented Dec 28, 2012 at 23:44
  • 3
    @kwatford: It returns the number of bytes python currently needs for the instance, yes, which happens to have a (small amount) of a correlation to how large the python integer is. But I doubt it is any use to the OP. Commented Dec 28, 2012 at 23:45

2 Answers 2

5

In Python3 there is the bit_length method:

>>> import math

>>> math.ceil(0x16.bit_length()/8)
1
>>> math.ceil(0x1234.bit_length()/8)
2
>>> math.ceil(0x12345678.bit_length()/8)
4
>>> math.ceil(0x1234567887654321.bit_length()/8)
8

In Python2.7, long integers also have the bit_length method:

>>> (10**20).bit_length()
67

>>> x = 123
>>> long(x).bit_length()
7
Sign up to request clarification or add additional context in comments.

10 Comments

This is fantastic and answers my question. I am from the C world and am porting a C program to Python. These values I mention represent register width and they come in all sizes. I need to know the size of the register, thank you very much unutbu
However one issue please ... I have to use the variable name, i cannot use the actual number right? In my case the variable name is always reg. reg can hold any one of the values, byte, word ... Just for an example I gave different names. reg.bit_length does not work. So how do I do do the same work with a variable name?
@SanthoshKumar: reg.bit_length() will work. Are you sure you need to worry about this in Python? I suspect you are doing too-literal a translation from C to Python, and there is an easier way to accomplish your goal. What will you do with these "integer sizes"?
In C this is the program:
Sorry ignore the above comment. My idea is to check if each bit is set from Right to left (LSB to MSB) and if the bit set then to print out the meaning of that bit. Like the error registers or status registers. for i in range(reg): shift = reg & (1 << i) the problem here is val will be the actual value of the number where as I want it to be number of bits
|
0

You can use sys.getsizeof, but the result may be annoying because there is a minimum that a integer can be.

For example, in my 64 bits PC all you variables have a size of 24 bytes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.