I always used python without thinking its memory management. But, I have a big question about how it deals memory.
def func1():
local_var = 1
print(id(local_var)) # memory address of local_var (stored in stack variable) -> 1
return local_var
# value copied, and local_var_different_scope points another memory address
local_var_different_scope = func1()
print(id(local_var_different_scope)) # -> 2
local_var_different_scope = 2
print(id(local_var_different_scope)) # -> 3
#include <stdio.h>
int main() {
int a = 1;
printf("%p\n", &a);
a = 2;
printf("%p\n", &a); // same as above!
return 0;
}
I would be really appreciated If you suggest any materials to understand this problem in depth. Thank you!
In Python all assignments are references to objects - in your case integer objects (everything is an object in Python!)
So in this snippet you've created a 1
(id=xyz
) object and returned that object (id=xyz
) to local_var_different_scope
. So local_var_different_scope
now references the id=xyz
object (which is the integer 1
you created).
def func1():
local_var = 1
print(id(local_var)) # memory address of local_var (stored in stack variable) -> 1
return local_var
You then reassigned local_var_different_scope
to reference a new integer object 2
(id=abc
). Naturally, the id
of 2
is different from 1
.
Hope this helps!
Simply put, it persists because the object returned is assigned to a variable, and Python is smart enough to know that!
Here's another interesting experiment:
def return_x():
x = 45
print(id(x)) # id=123
return x
fake_x = return_x()
print(id(fake_x)) # id=123
print(x) # NameError 'x' is not defined
One might then guess that since x
is returned, it can be referenced - but that is not the case!
To drive the point home, each assignment is a reference to an object in Python. x
simply referenced the object 45
with id=123
!
return x
returned the referenced object but not the variable x
!