C++ is a language that allows creation of static instances of objects, this is without using pointers. This is why copy-constructors are needed, since it is common to copy or clone objects. Other languages like Delphi don’t allow to create static variables to instantiate objects. Instead, all objects are pointers. So in C++, assignments of instances of the same class create a copy of the object, while in Delphi, only the reference to the second instance is copied.
When I started learning Python one year ago, I wanted to know if objects were cloned on assignments or if only the reference to the object was assigned instead. Here is how I found it:
-
class Item():
-
def __init__ (self):
-
self.text = ""
-
def sayIt(self):
-
print self.text
-
-
A = Item()
-
B = Item()
-
A.text = "testing A"
-
B.text = "testing B"
-
A.sayIt()
-
B.sayIt()
-
A = B
-
A.sayIt()
-
B.sayIt()
-
A.text = "testing A 2"
-
A.sayIt()
-
B.sayIt()
This code will output 6 messages. The first two will be obviously different, since A and B are to separate instances on memory. When B is assigned to A, two things can happen. B is cloned and A is a new instance equal to B or the reference to B is assigned to A so A and B become the same instance in memory (B). No matter which one happens, both messages after the assignment will correspond to the B instance (“testing B”).
But what will happen when we modify A again? Will B be also modified?
Here is the output:
testing A testing B testing B testing B testing A 2 testing A 2
So A and B are now the same object in memory.

It reminds me about Politecnica ;). By the way, will you come on Christmas? Too much time since I last saw the two of you…
Yeah… sometimes it is very easy to forget about things like these. I like to test them to be sure :)
Regarding Christmas, nope, we are not going. Out little baby is still to small for traveling so far.
But we are planning to go in April or May, so stay tuned :)