Everything is a pointer perspective
Posted on September 2, 2022
Tags: codeetc
Everything in python is a PyObject
Assignment in python are pointers binding.
- Reassignment of whole objects, DOES NOT mutate current objects; It CREATES new literal objects then binds.
a = 4
basically the same asa = new num(4)
- Reassignment of properties like list elements or object properties DOES mutate current objects.
a.age = 4
1 Reassignment to whole objects
= 1 # a points to num(1)
a = a # b points to The object a points to, num(1)
b = 2 # a points to a new object num(2)
a print(b)
class num():
def __init__(self,x):
self.x = x
= num(1)
a = a
b print(b.x) #1
= num(2)
a print(b.x) #1
a
DOES NOT CAUSE OBJECT MUTATION FROM num(1) to num(2)
Instead we just pointed to a different object.
int* a = new int(1);
int* b = a;
= new int(2);
a << *b << endl; // prints 1 cout
2 Reassignment of object properties
# notice this doesnt cause mutation since we assign WHOLE objects
class num():
def __init__(self,x):
self.x = x
= num(1)
a = a
b print(b.x) #1
= num(2) #ASSIGN WHOLE OBJECTS
a print(b.x) #1
# but notice this does cause mutation since we assign PROPERTIES of object
class num():
def __init__(self,x):
self.x = x
= num(1)
a = a
b print(b.x) #1
= 2 #ASSIGN PROPERTIES OF OBJECT
a.x print(b.x) #2
int* a = new num(6);
int* b = a;
<< *b << endl; //6
cout ->x = 7;
a<< *b << endl; //7 cout