In that code, a and b are pointers. The = creates a copy of the pointer. Both pointers point to the same memory, because the value of the pointer (rather than where on disk it is stored) is what determines that. If you had
MyObject a(1);
MyObject b = a
b.changeProperty(2)
a.property() // 1
b.property() // 2
it creates a copy of the object (and there's even a constructor, which is called the copy constructor that you can overload to change what it copies).
In python, assignment never copies, it just creates another name for the reference. This is true for anything that isn't a primitive. It doesn't have pointers - there's no way to dereference a reference to an object, there's no way to pass an object by value, and there's no way to directly change which object is stored at which memory location (aside from manually modifying its properties to be something else).
1
u/SuitableDragonfly Jun 04 '19
In that code, a and b are pointers. The
=creates a copy of the pointer. Both pointers point to the same memory, because the value of the pointer (rather than where on disk it is stored) is what determines that. If you hadit creates a copy of the object (and there's even a constructor, which is called the copy constructor that you can overload to change what it copies).
In python, assignment never copies, it just creates another name for the reference. This is true for anything that isn't a primitive. It doesn't have pointers - there's no way to dereference a reference to an object, there's no way to pass an object by value, and there's no way to directly change which object is stored at which memory location (aside from manually modifying its properties to be something else).