Alright, so Python is not pass-by-value and also not pass-by-reference, so what is it..?
Well, in order to understand what Python does, we need to understand well what Python objects are!
By the way, did you know that everything in Python is an object?
Each Python object is characterised by three things...
π its identity (an integer that identifies your object, just like your social security number);
π a type (that identifies what operations you can do with your ojbect); and
π some content;
Here's an example π
Now, what might be tricky here, is that the βnameβ `obj` has NOTHING to do with the object itself.
The three letters βobjβ were just a label that I attached to the object with identity 2698212637504, type list, and contents 1, 2, 3.
I can attach many names to it π
But these names are just labels I put on the exact same object.
How can I know it's the exact same object?
Well, all their βsocial security numbersβ match π
Therefore, these labels all point to the same name...
So...
Oh, and by the way, this is what the `is` operator does in Python π!
It checks to see if two labels (two names) are pointing at the _same_ object.
In other words, it checks if the identities of the two operands are the same!
So this is what assignment does: it just sticks a new label β a new name β onto an object.
It's pretty much like a nickname!
I have plenty of different nicknames, but regardless of what you call me, or what my mom calls me, I'm always the same person, right..?
Therefore, if I change, everyone sees the change, not just my mom, or not just my friends, or not just you!
So, if I mutate the _contents_ of the list we had previously...
Then all the nicknames should be able to tell that something is different π
Now, at this point, the identity of the object did NOT change!
It's still the same object!
It's just that its contents changed a bit.
Much like you can change your clothes, or have different thoughts.
That's because a list is a _mutable_ object..!
_Mutable_ means that it can be mutated β that it can be changed (internally).
Some other Python types are _immutable_, which means they cannot be modified internally.
A good example of that is the `tuple` type.
If you create a `tuple`, you can't change its elements π
Thus, with immutable data types (like tuples, strings, integers, floats) you can only _build_ new objects, you cannot modify them internally.
It's like that VERY stubborn friend of yours.
It's always the same object, and their contents don't change π
Ok, cool, but so what? π€ͺ
When you call a function in Python, the model you use is βpass-by-assignmentβ.
This means that the parameters of the function just become new labels to the objects you gave as arguments.
How can we tell this..?
If you've been paying attention, you should know by now!
You can check that it's the same objects, just with a different label, because the identity of the objects is the same.
Here's a function that prints the identity of its only argument π
As you could see, being (im)mutable didn't matter: when we call `foo`, βxβ just becomes a new label to the exact same object we had _outside_ of the function.
So that means the following:
β if you give mutable objects to your function, your function can change the inner contents of your objects.
That's what sometimes trips people up.
The classical examples in Python π are lists and dictionaries.
Careful when using them inside your function π
I think this is it for this thread π In < 24 hours I will publish a Pydon't on my blog (mathspp.com/blog/pydonts) with all this information, more examples, and also all the sources.
This way you'll be able to do some research on your own, as well.
If you like the way I explain these interesting Python π concepts, and you'd like more...
Make sure to follow me (@mathsppblog) to not miss a single drop of knowledge I share!
I would also love it if you could retweet the beginning of this thread πͺπ₯:
I hope that you found this thread very informative, and I'll gladly take any questions you might have ππ¬
Here's a TL;DR:
π python uses pass-by-assignment (not pass-by-value, not pass-by-reference);
π all objects have 3 different characteristics: an id, a type, contents;
π `is` checks if two names point to the same object;
π when calling functions, parameters become labels to the objects that were passed in;
π for mutable objects (that can have their contents changed), the function can make changes that are visible from the outside.
Bye π
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh