python3
python3
Type: Integer
Name: x Data: 3
Ref: <address1>
>>> x = x + 1
Understanding Reference Semantics
When we increment x, then what happening is:
1. The reference of name x is looked up.
2. The value at that reference is retrieved.
3. The 3+1 calculation occurs, producing a new
data element 4 which is assigned to a fresh
memory location with a new reference
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4
>>> x = x + 1
Understanding Reference Semantics
When we increment x, then what happening is:
1. The reference of name x is looked up.
2. The value at that reference is retrieved.
3. The 3+1 calculation occurs, producing a new
data element 4 which is assigned to a fresh
memory location with a new reference
4. The name x is changed to point to new ref
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4
>>> x = x + 1
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3
a = [1, 2, 3] a 1 2 3
a
b=a 1 2 3
b
a
a.append(4) 1 2 3 4
b
Surprising example surprising no more