On Windows 10, I am using Pycharm to debug code.
My example code is:
a=3.25
b=int(a)
as we know, it is very easy for us the get the address of a and b, and then check the binary digit in memory in C language.
I would like to check what the binary digital of a and b in memory is in Python.
how to do that?
[quote Ardeal]
“as we know, it is very easy for us the get the address of a and b, and then check the binary digit in memory in C language.”
[end quote]
I don’t know who “we” is, but I don’t know what you mean.
Do you mean showing the numbers in base 2 (binary) instead of base 10 (decimal)?
Python supports base conversion to binary for integers, and we can get the hexadecimal (base 16) representation of floats:
a = 3.25
b = int(a)
a.hex() # returns '0x1.a000000000000p+1'
bin(b) # returns '0b11'
Converting hex into binary is left as an exercise. (Hint: 3.25 in binary will be 11.01.)
Thank you for you reply!
The method you mentioned is not what I want to know.
Please check the following c code:
I want to do similar thing in Python as C
For the following C code, we could check the binary number of float a in memory, which is very easy in C language. I suspect you know that.
float a = 3.25;
unsigned in address_a = (unsigned int)(&a)
This is a Python forum, not a C forum. Please don’t assume that Python programmers are expert in C.
I have no idea what your C code does.
Also, it is not safe to assume that Python objects have a fixed address. In at least two Python interpreters, and probably more, objects can move around in memory at any time. The Python language does not define any sort of “get the address of this object” function or operator.
Some people will tell you that you can use id(). That is wrong. It is not portable: in Jython and IronPython, id() returns consecutive values 1, 2, 3, 4, 5, 6, … as needed.
To access the real bytes in memory containing a Python object you would need to write a C extension for that. This is not normally needed in Python so there is probably no such a maintained extension. This has been covered in this question:
I’m curious, @ardeal, are you wanting to look at the raw contents of the variable’s memory address or just convert to binary?
I ask because looking at the native binary would be faster than a conversion in some languages. Not sure if that’s the case for Python (and it’s very doubtful from what Steven D’Aprano said in the post above).
what I want to do is to look at the raw contents in variable’s memory. I don’t want to convert the variable to binary.
In my post, I gave an example about how I look at the raw contents in variable’s memory in C language.
according to those upper guys explanation, in python, it is not easy the look at the raw content in variable’s memory.
Your first problem is working out how to get the address of a variable.
Variables don’t have an address in Python. Only objects have addresses, and there is no supported way to get an object’s address. In CPython you can use id() and hope that the implementation hasn’t changed, but that’s not portable. In Jython, IronPython and maybe PyPy you are out of luck.
Once you have the address, you can probably write a C extension to dump the raw bytes of an object. But why? What are you planning to do with it?
I am not much familiar with numpy. I tried to find the method numpy.float64.view() in the documentation but I gave up. Can you please point me to this method’s description?
I used this function last Fall on a vision project to work with a cropped part of an image (or at least considered using it; I’d have to look up the code). view() is a virtualized child object of the array, showing only part of the data. [EDIT: ] Perhaps better stated as “a view() is a pointer to part of an existing array.”
It’s actually on the other side of the array object from the hardware register, so is farther away.