Understand the weirdness. How Python Passes Arguments.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on clarity coders in today's video we're going to do something a little different i read an article that came out in python weekly talking about how python passes arguments into functions now i think this is extremely important for every level of python programmer because people get it messed up so often they tend to think it's passed by value or past by reference and it's neither let's not waste any more time and jump right in [Music] all right so this article is really interesting because it uses examples to prove first why python is not passed by value and then why it's not passed by reference so let's go over these examples real quick so i'm going to create a new replica instance you can do this locally if you want the code along it's fun little exercise along the way if you're advanced stick with us comment below what i messed up or anything like that all right so my example is going to be pretty easy so what i'm going to do here is i'm going to set my age i'm going to say my age equals 32 then i'm going to try and change that value in a function so i'm going to create a function called make me young and i'm going to pass in my age then finally i'm going to print out my age so of course we have to define that function above let's define that saying we're passing in age and then inside this function we can do something to age itself so inside this function let's say age equals 26. i think that's the prime right i'm going to i'm currently 32 i'm gonna run this function pass in age it's going to change my age to 26 and then we're going to print out age and see what happens so if we run this you'll see we get back 32 so our change inside the function did not affect our age below here so what this is saying to me or what this is an example of is that python might be passed by value this age variable that we've assigned to 32 when we pass that into the function it's not passing in that reference it's just saying hey here's 32 and all that happens without changing the actual value stored inside of our age variable so this makes it seem to a lot of people like python is a pass by value i'm going to comment this out so i'm going to shift press control and then the forward slash so i commented all those out so now we can see that we think python is a pass by value it can't be passed by reference because 32 didn't change if it was passed by reference that should have changed so let's do another example here so i'm going to say my friends equals and i'm going to create a list of all my friends say orion who's a co-worker not really a friend zane who's my brother and my wife so out of my three friends they're all relatives or co-workers fantastic so now i have three friends here i'm going to create a function again and i'm going to call it change friends and i'm going to pass in all my friends to that function and then we're going to print out all my friends awesome so we've decided that based on above python is not passed by reference and it's probably passed by value so let's define this function let's call it def change friends let's pass in friends and inside this function let's manipulate my friends that doesn't sound great so friends let's change the first friend orion who's a great dude but we're gonna eliminate him from my friends list so i'm changing out orion so i'm changing out the position zero friend which is that first in the list i'm changing it to bob instead of orion now what we learned above is that we think python is passed by value so we're assuming here when we print this out our friends list is going to say orion zane and cat still because anything happening here is just we're just passing in the value so it shouldn't change right and if we run this you'll notice that it does change so what the heck is going on here well that's because python is neither passed by value or passed by reference it's actually passed by assignment now to understand that we have to take a quick look at the difference between immutable and mutable objects in python so i'm going to create a new replit here python again so in python everything is an object and these objects have three attributes that we can track the id of the object that's where the actual object is so it's a unique identifier for the object itself the type of the object and then the value of the object now what we're interested in here is the type of the object that determines whether it's immutable or a mutable object so let's look at an example here let's stick with a mutable object so a mutable object is something like a list which makes sense mutable means we can change what's inside this object so if we go with the exact same example i think i might the order of friends might be different here we can now print out the id value of our friends list and we can print out the type if we wanted or anything else so let's go ahead and run this and if we run this you'll see that we get an id value and the class perfect so now let's also make a change to this list now remember it's mutable so we're going to change just like we did in the example the zero index we're going to set that equal to bob and then i'm going to copy these exact lines down below and print them and run it again and you'll notice something interesting here that even though we change this object and it now contains different values because now it has bob zane and cat it still has the exact same id and if we wanted to show that it did change we could actually print out our friends list as well cool run this one more time and you'll see that we did in fact change the list but the id value stays exactly the same now how is this different from our other example so if we do print here we slash n now let's do our other examples so in my other example we had age equals 32 and we can do something similar we can print out the id of age we can print out the type of age and let's actually print out age itself then let's make a change to age so in this case let's say plus or equals one so maybe in the program or something you're incrementing the age by one so let's run this and we'll ignore the top part it should stay the same and the bottom part here is our immutable objects so if you look at this we have an id a type which is int and then the original value of our age which is 32. integers are immutable objects meaning we can't change them we can only create new objects so you'll see below here in order to increment age to 33 it gave us a whole new object id so it's creating a brand new object it is not changing 32 to 33 it's just pointing at a whole new value here okay so that probably makes sense to you so now back to our original examples how is python passing in these arguments and why does it work the way it does so i'm going to hop back to my other arguments replied that i created here so why does this work the way it does let's first look at our pass our immutable object here example so the immutable object example i'm passing in age of 32. it is passing in the actual assignment of age so that id value so if that id value you know it's a long number but say it was one two three right now it's still one two three but as soon as i try to change that it's immutable so it has to change that object reference so it creates a new object reference say 345 and points that at 26. so when we print out age down here it's still referencing the original id on the object of one two three in my made up example so it still prints out 32. now in the below example it again passes in the original reference to the object so say our friend's object's original id was 123 it passes that in to the change friends function now it's a mutable object so it doesn't have to create a new object so it changes the zero index to bob on the original object of one two three when we go down to print it this friends is still pointing at that id of one two three so we actually see the change to our screen so that's the difference it's not passing by value it's not passing by reference it's passing by assignment and you get different looking results when you pass things into a function based on whether it's an immutable or immutable object i hope you guys like this video i know it's a little different format so i'm gonna throw the link to the actual article below check that out as well let me know what you think in the comments and any concerns you have with the presentation and until next time keep coding
Info
Channel: ClarityCoders
Views: 1,558
Rating: undefined out of 5
Keywords: immutable objects vs mutable, python immutable, python mutable, python weekly
Id: D3IffJOwDY0
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Tue Nov 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.