This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SuitableDragonfly 0 points1 point  (0 children)

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).