[Memo] The mystery of cumulative assignment statements in Python functions

What is a cumulative assignment statement?

Cumulative assignment statement

The cumulative assignment statement is a combination of binary operations and assignment statements into a single statement.

Mystery

>>> a = 1
>>> a = a + 1   
>>> a
2
>>> a = 1
>>> def f():
...     b = a + 1
... 	return b
... 
>>> f()
2
>>> a = 1
>>> def f():
... 	a = a + 1
... 	return a
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
UnboundLocalError: local variable 'a' referenced before assignment

Addendum 1

>>> a = 1
>>> def f():
... 	global a
... 	a = a + 1
... 	return a
...  
>>> f()
2

Addendum 2 Solving the mystery

Why do I get an UnboundLocalError when a variable has a value?

According to it, it seems to be explained like this.

This is because when you assign to a variable within a scope, that variable becomes local to that scope, hiding variables of the same name in the outer scope.

Thanks to @gotta_dive_into_python

Addendum 3 And to a new mystery

I'm experimenting that "when assigning to a variable in a certain scope, that variable becomes local to that scope" is the same as being registered in locals (). There is another mystery in my house.

>>> def f():
... 	a = locals()
... 	return a
... 
>>> f()
{}
>>> def f():
... 	a = locals()
... 	b = 1
... 	return a
... 
>>> f()
{}
>>> def f():
... 	a = locals()
... 	b = locals()
... 	return a
... 
>>> f()
{'a': {...}}

Recommended Posts

[Memo] The mystery of cumulative assignment statements in Python functions
Check the behavior of destructor in Python
Summary of various for statements in Python
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Get the update date of the Python memo file.
[Python] Summary of functions that return the index that takes the closest value in the array
[Python] Memo about functions
the zen of Python
# 4 [python] Basics of functions
Experience the good calculation efficiency of vectorization in Python
The simplest Python memo in Japan (classes and objects)
How to get the number of digits in Python
[python] Get the list of classes defined in the module
The story of FileNotFound in Python open () mode ='w'
The pain of gRPC using Python. November 2019. (Personal memo)
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
Python Note: The mystery of assigning a variable to a variable
Ssh connection memo using ProxyCommand of ssh_config in Python
A memo that implements the job of loading a GCS file into BigQuery in Python
Try scraping the data of COVID-19 in Tokyo with Python
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
Towards the retirement of Python2
Download the file in Python
Measure the execution result of the program in C ++, Java, Python.
Check the operation of Python for .NET in each environment
Find the difference in Python
The result of Java engineers learning machine learning in Python www
Calculate the square root of 2 in millions of digits with python
About the ease of Python
Have the equation graph of the linear function drawn in Python
Learn cumulative sum in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Get the number of specific elements in a python list