I understand the differences between passing by value and passing by reference , but doing some research about Python found that it doesn't use the aforementioned models, but rather one called "pass by object". It appears to be similar to the model used in LISP.
What does pass by object mean? What are the differences between passing by object, by value, and by reference?
I found a similar question, maybe the answer they gave will help you.
Understanding the absence of pointers in Python
In the other answer, it says this.
So in a comment they answer this.
First, definitions (mine, since I come from the world of C++, where there is no passing by name/object, so I adapt the definitions a bit to be able to compare them):
With this, let's move on to the differences (under the assumption that we call a function, whose parameter is received by value, reference or name/object respectively):
Pass by value : creates a new variable, whose object is a copy of another (two different boxes with the same value/content; assign modifies the content of the local box).
Pass by reference : a reference to the received variable is created (that is, another name is created for the received object). To manipulate the reference, is to manipulate the "shared object" (two names for the same box; assign modifies the content of the shared box).
Pass by object (or by name) : a new variable is created, pointing to the received object (two boxes pointing to the same object; assign makes the local box point to a new object, making the original object no longer shared ).
The difference with the previous one is that, being a new variable, it is reassignable, which means that
a = o
(if ita
is a variable received by name, ando
any type of variable or reference), from now on, it makes ita
point too
(a its associated object) without modifying the shared object (which will no longer be shared from now on).However, if it
a
were a reference or a normal variable,a = o
it causes the value of the object pointed to bya
, to be substituted for the value of the object referred to byo
. A reference always shares the object sent when the reference is created, and this cannot be "avoided" or "changed" in any way (a reference cannot become the name of a different box).In passing by name, however, each time an assignment is made, the value of the pointed object is not modified, but rather the new object is pointed to, leaving the original object orphaned if it was its "last box" (which then, the garbage collector should take care of deleting).