0

If I fire a terminal with Python and I write:

>>> x = 100
>>> x == 100
True
>>> x is 100
True

I can easily see that the boolean works perfectly for both == and is But if I try to set 1000 as variable as result I have:

>>> x = 1000
>>> x == 1000
True
>>> x is 1000
False

Is there a way for me to see how the variable is stored on the memory bit by bit? I don't want the memory pointers, I would like to see the 0 and the 1 to see what Python is storing exactly.

  • 100 in decimal = 01100100 in binary (8 bits = 1 octet)
  • 1000 in decimal = 0000001111101000 in binary (16 bits = 2 octets).

100 fits in a 8 bits octet while 1000 cannot fit into it. From my understanding == always gives a TRUE because it is reading a value while is is reading a memory space, so eventually it is reading just the first 8 bits of the octet.

Community
  • 1
  • 1
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • 2
    Do you know the difference between `==` and `is`? The way you are using `is` is implementation specific, as is the way that integers (and any other object) are stored. – cdarke Nov 06 '16 at 08:26
  • 2
    Check this http://stackoverflow.com/questions/14144707/and-is-in-python – Siva Shanmugam Nov 06 '16 at 08:27
  • Possible duplicate of [Python int to binary?](http://stackoverflow.com/questions/699866/python-int-to-binary) – cdarke Nov 06 '16 at 08:28
  • Dear @cdarke: I know that `==` is reading a **value** while `is` is reading a **memory space**. In binary 100 is represented as 01100100 (8 bits) while 1000 is represented as 0000001111101000 (16 bits). While 100 can fit into a 8 bit array everything is fine but 1000 fits into 2 arrays and when `is` goes to read the memory it just read the first of 8 bit array. – Francesco Mantovani Nov 06 '16 at 09:32
  • Dear @Siva Shanmugam, I know this topic has been raised before but my goal is to find a way to read **bit by bit** what Python is recording on the memory. – Francesco Mantovani Nov 06 '16 at 09:34
  • You can see the binary representation of a Python integer `n` with `bin(n)`, but I guess that's not what you're asking. Bear in mind that an integer in Python is a fully-fledged object, and that's why it's possible to have two different integer objects that both have the value of 1000. – PM 2Ring Nov 06 '16 at 09:36
  • If you can read C, you may find it interesting to look at the CPython source code of [intobject.c](https://hg.python.org/cpython/file/39803c20c9bf/Objects/intobject.c) – PM 2Ring Nov 06 '16 at 09:39
  • 1
    The `is` operator performs an identity check on its operands, so `a is b` is equivalent to `id(a) == id(b)`. In CPython, the `id` function returns the memory address of its argument, but that's just an implementation detail. In principle, `id` _could_ return some other unique identifier of an object, the memory address just happens to be convenient for this purpose. – PM 2Ring Nov 06 '16 at 09:46
  • Thank you @PM 2Ring, your reply is partially right but as this happens just with variables that have more than 8 binary bits we have to dig deeper – Francesco Mantovani Nov 06 '16 at 09:58
  • 1
    The reason for the behaviour you're seeing is that the CPython interpreter pre-allocates integer objects for integers in the range -5 to 256 (inclusive). Whenever an expression evaluates to an integer in that range you get one of those pre-allocated objects. See http://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers also http://stackoverflow.com/questions/15171695/weird-integer-cache-inside-python – PM 2Ring Nov 06 '16 at 10:01
  • This is partially the [answer] (http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python) but I still don know how to print a variable and see its bits – Francesco Mantovani Nov 06 '16 at 10:28
  • Variable names in python are references to objects, so `is` is used to see if two names refer to the same object. `==` can be overloaded by the class, `is` cannot. Printing a variable to "see its bits" is not meaningful in a high-level language because the implementation is abstracted. The best you can do is to look at an implementation's source code if you want to see how it is abstracted. If you do that, remember that it could change without warning between versions. – cdarke Nov 06 '16 at 10:45

1 Answers1

0

Eureka! Thank you all for your help!

I now understand that this is not regarding how Python stores the informations in binary code but about how Python stores per-allocated integer objects for integers in the range -5 to 256 (inclusive):

>>> x = 256
>>> y = 256
>>> id(x)
22980640
>>> id(y)
22980640

but then:

>>> x = 257
>>> y = 257
>>> id(x)
23550352
>>> id(y)
23550328
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113