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 = 4basically 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
a = 1 # a points to num(1)
b = a # b points to The object a points to, num(1)
a = 2 # a points to a new object num(2)
print(b)class num():
def __init__(self,x):
self.x = x
a = num(1)
b = a
print(b.x) #1
a = num(2)
print(b.x) #1a 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;
a = new int(2);
cout << *b << endl; // prints 12 Reassignment of object properties
# notice this doesnt cause mutation since we assign WHOLE objects
class num():
def __init__(self,x):
self.x = x
a = num(1)
b = a
print(b.x) #1
a = num(2) #ASSIGN WHOLE OBJECTS
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
a = num(1)
b = a
print(b.x) #1
a.x = 2 #ASSIGN PROPERTIES OF OBJECT
print(b.x) #2int* a = new num(6);
int* b = a;
cout << *b << endl; //6
a->x = 7;
cout << *b << endl; //7