Search code examples
pythonmemory-address

What is the difference between the address value from instance and the address value from id(instance) in python?


class T:
    pass


t = T()

print(t) # <__main__.T object at 0x100651460>
print(id(t)) # 4301591648

What is the difference between 0x100651460 and 4301591648 in the code above? id(t) is to print the address value of the object as everyone knows. However, <main.T object at 0x100651460> also implies that 0x100651460 is the address value of the object. Which one is the real address value?


Solution

  • They are the same number. There is no difference in value, the only difference is that the first one is represented in hexadecimal, and the second one is represented in decimal.

    >>> hex(4301591648)
    '0x100651460'
    >>> 0x100651460 == 4301591648
    True
    

    The prefix 0x is commonly used to indicate a hexadecimal representation.