filmov
tv
Python Tutorial: Pass by assignment

Показать описание
---
The way that Python passes information to functions is different from many other languages. It is referred to as "pass by assignment", which I will explain in this lesson.
Let's say we have a function foo() that takes a list and sets the first value of the list to 99. Then we set "my_list" to the value [1, 2, 3] and pass it to foo(). What do you expect the value of "my_list" to be after calling foo()? If you said "[99, 2, 3]", then you are right. Lists in Python are mutable objects, meaning that they can be changed.
Now let's say we have another function bar() that takes an argument and adds ninety to it. Then we assign the value 3 to the variable "my_var" and call bar() with "my_var" as the argument. What do you expect the value of "my_var" to be after we've called bar()? If you said "3", you're right. In Python, integers are immutable, meaning they can't be changed.
Let's look at another example to understand what's going on. Imagine that this gray bar is your computer's memory.
When we set the variable "a" equal to the list [1, 2, 3], the Python interpreter says, "Okay, now 'a' points to this location in memory."
Then if we type "b = a", the interpreter says, "Okay, now 'b' points to whatever 'a' is pointing to."
So if we were to append 4 to the end of "a", both variables get it because there is only one list.
Likewise, if we append 5 to "b", both variables get it.
However, if we assign "a" to a different object in memory, that does not change where "b" is pointing. Now, things that happen to "a" are no longer happening to "b", and vice versa.
How does this relate to the example functions we saw earlier?
When we assign a list to the variable "my_list", it sets up a location in memory for it.
Then, when we pass "my_list" to the function foo(), the parameter "x" gets assigned to that same location.
So when the function modifies the thing that "x" points to, it is also modifying the thing that "my_list" points to.
In the other example, we created a variable "my_var" and assigned it the value 3.
Then we passed it to the function bar(), which caused the argument "x" to point to the same place "my_var" is pointing.
But the bar() function assigns "x" to a new value, so the "my_var" variable isn't touched. In fact, there is no way in Python to have changed "x" or "my_var" directly, because integers are immutable variables.
There are only a few immutable data types in Python because almost everything is represented as an object. The only way to tell if something is mutable is to see if there is a function or method that will change the object without assigning it to a new variable.
Finally, here is a thing that can get you into trouble. foo() is a function that appends the value 1 to the end of a list. But, whoever wrote this function gave the argument an empty list as a default value. When we call foo() the first time, we get what you would expect, a list with one entry. But, when we call foo() again, the default value has already been modified! If you really want a mutable variable as a default value, consider defaulting to None and setting the argument in the function.
You can check your understanding of the following exercises.
#DataCamp #PythonTutorial #WritingFunctionsinPython
Комментарии