29
loading...
This website collects cookies to deliver better user experience
id()
function. For those of you who don't know what id()
is:The id()
function returns a unique ID of the object. All objects in python have a unique ID and no 2 different values correspond to the same ID.
a = b = 500
print(id(a) == id(b))
Fun fact: In python, id(a) == id(b)
is analogous to a is b
.
True
because python creates the variable b
with the value 500
and then creates a variable a
pointing to the value of b
. This implies that a
and b
are pointing towards the same memory location and hence the same ID.a = 500
b = 500
print(id(a) == id(b))
False
because: a
pointing to the value 500
in the memory. b
pointing to another value 500
(yeah, both 500 are different).a = 50
b = 50
print(id(a) == id(b))
False
" but not so fast.<class 'int'>
) are shared. This is done entirely to save space. The memory imprint of the console would be significantly larger if these objects weren’t sharing their memory.True
a = 500
id1 = id(a)
a = 500
id2 = id(a)
print(id1 == id2)
False
. I'll explain to you why. When you re-declare a variable in python, the interpreter works in the same way as a declaration. i.e. It entirely deletes the before existing value and creates a variable with the new value. So when we give a = 500
, the second time the interpreter deletes the previously existing value of a
and create a new memory location for 500
where a
would point towards. Both these IDs are most likely to be different.False
.True
. this is because number belonging to the inclusive range have a fixed memory location.