[Python beginner] Variables and scope inside the function (when the processing inside the function is reflected outside the function and when it is not reflected)

I had a vague understanding of variable scope in functions in Python, so I'll share it for anyone who stumbled upon something similar. What I want to say is that if you make a whole assignment to a variable prepared as an argument of the __ function, it will be treated as if a new local variable is defined, and if you make an assignment to an element of the variable. Please note that there is a difference that it is treated as a global variable as it is __.

In the code below, we are manipulating two lists list_a and list_b inside the function, but only list_b is treated as a global variable and has an effect outside the function.

def func(list_a, list_b):
    list_a = list_a + ['hoge']
    list_b[1] = list_b[1] + 10


a = [1, 2, 3]
b = [5, 6, 7]

func(a, b)
print(f"a={a}, b={b}")
func(a, b)
print(f"a={a}, b={b}")

The output will be:

a=[1, 2, 3], b=[5, 16, 7]
a=[1, 2, 3], b=[5, 26, 7]

In Python, an expression of the form "a = ..." in a function is treated as a newly defined local variable a that has scope only inside the __ function. Therefore, in list_a = list_a + ['hoge'] in the function func, the list_a on the left side is treated as a new definition of the local variable list_a (list_a on the right side is given as an argument of the function and is outside the function. Is also a variable with scope). Therefore, it has no effect on a outside the function.

On the other hand, for list_b [1] = list_b [1] + 10, list_b [1] on both sides must be treated as the first element of the __ already defined __ variable list_b. In this example, list_b is an argument of the function func, a variable already defined outside the function. Therefore, the processing by func is reflected in the variable b outside the function.

The same is true for variables of other types such as ndarray and pandas.DataFrame.

Recommended Posts

[Python beginner] Variables and scope inside the function (when the processing inside the function is reflected outside the function and when it is not reflected)
Test.py is not reflected on the web server in Python3.
Check the scope of local variables with the Python locals function.
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
Build Python environment on Ubuntu (when pip is not the default)
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
Processing when the key input of Python pygame does not go well.
python note: when easy_install is not available
[Python] Use and and or when creating variables
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
It seems that the version of pyflakes is not the latest when flake8 is installed
[Python] I want to know the variables in the function when an error occurs!
[Python] Precautions when retrieving data by scraping and putting it in the list
Make a Python program a daemon and run it automatically when the OS starts
[EC2] What to do when selenium is stuck and processing does not proceed
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Python Pandas is not suitable for batch processing
The answer of "1/2" is different between python2 and 3
[Xonsh] The Python shell is sharp and god
About the difference between "==" and "is" in python
[Python Data Frame] When the value is empty, fill it with the value of another column.
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
I want to replace the variables in the python template file and mass-produce it in another file.
There is a pattern that the program did not stop when using Python threading
[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2
The timing when the value of the default argument is evaluated is different between Ruby and Python.
It is easy to execute SQL with Python and output the result in Excel