You can use module 'memory_graph':
https://pypi.org/project/memory-graph
to see the call stack. Here stack frame "0: <module>" holds all the global variables and stack frame "1: f" holds the local variables of the f() function.
In the first program we see that after calling the f() function variable local variable 'a' shares its data with global variable 'b' of the calling frame. Therefore if 'a' is changed 'b' is also changed.
Now the second program:
Here f() is called with 'b[:]' (list slicing), so a copy of its data is send to f(). Therefore the local variable 'a' does not share any data with global variable 'b' of the calling frame and when 'a' is changed is does not change 'b'.
Full disclosure: I am the developer of memory_graph.
https://pypi.org/project/memory-graph
to see the call stack. Here stack frame "0: <module>" holds all the global variables and stack frame "1: f" holds the local variables of the f() function.
import memory_graph # see install instructions at link above id(b) def f(a): a[0]=3 print("a: ",a, "id(a): ",id(a)) memory_graph.show( memory_graph.get_call_stack() ) # draw call stack f(b) print("b , id(b)",b,id(b))
In the first program we see that after calling the f() function variable local variable 'a' shares its data with global variable 'b' of the calling frame. Therefore if 'a' is changed 'b' is also changed.
Now the second program:
import memory_graph id(b) def f(a): a[0]=3 print("a: ",a, "id(a): ",id(a)) memory_graph.show( memory_graph.get_call_stack() ) # draw call stack f(b[:]) print("b , id(b)",b,id(b))
Here f() is called with 'b[:]' (list slicing), so a copy of its data is send to f(). Therefore the local variable 'a' does not share any data with global variable 'b' of the calling frame and when 'a' is changed is does not change 'b'.
Full disclosure: I am the developer of memory_graph.