Call by Reference of memory, Newbie talk

Mart The Martian
2 min readMar 7, 2018

--

Pointer: a block of memory pointing to another block of memory. In python and js, variable is like the pointer, it’s a block of memory storing the address to another block of memory, which is the data “assigned to the variable”.

Example 1: m is the pointer memory in this case. [] is the data in this case. [] is mutable.

def f(l):
l = [1]
m = []
f(m)
print(m)

when m=[] runs, 1, the data [] is created at physical address #0001, 2. variable m is created at physical address #0002. The block of memory #0002 stores the address of [], that’s what the = sign does for variables and data, it pimps, it hooks, it marries them, but they are not the same thing!

when f(m) runs, m (the #0002) block of memory copied and created the variable l at physical location (#0003). Now the memory #0003 is storing (#0001) as well. so, at this moment, l and m both have address (#0003) stored inside, but they are different variables.

Inside the function scope, = is working again, pimping, hooking, marrying the l and new data [1]. this time data [1] is created and stored at location $0004, then $0004 is stored in the memory block of $0003 of l.

[]:$0001

m:$0002 {$0001}

l:$0003{$0001}

[1]:$0004

after l = [1]:

l:$0003{$0004}, but m: $0002{$0001}

the reference of $0001 is passed around, the memory of $0002 is copied(by value, which is an address, confusing),

Example II: $0001 is mutated.

def f(l):
l.append(1)
m = []
f(m)
print(m)

[]:$0001

m:$0002 {$0001}

l:$0003{$0001}

l.append first , retrieves the data at physical position $0001, and finds out it has a method called append, which took a number in and modified the mutable data [] at $0001 => $0001*

m: $0002{$0001*}

l: release after function scope ended.

this entire process, we are treating [] and [1] as one object. as a matter of fact, they each have their parts…

the reason $0001 is mutable is because it’s composed of parts that aren’t mutable, and mutation is done through re-assigning the address internally……

--

--