Difference between nonlocal and global in Python

The global statement refers to the outermost variable. The nonlocal statement refers to a variable one outside the function.

#
#Interactive mode>>>To
#You can do it by copying.
#

interval= 0          # <---The global statement refers to the outermost variable.

def Make a counter():
Current value= 0  # <---The nonlocal statement refers to a variable one outside the function.
def count():
global interval
nonlocal current value
Current value=Current value+interval
return current value
return count

Count=Make a counter()

#interval=Since it is 0, it remains 0
Count()
Count()
Count()

#interval=If set to 1, it will be incremented by 1.
interval= 1
Count()
Count()
Count()
>>> #interval=Since it is 0, it remains 0
>>>Count()
0
>>>Count()
0
>>>Count()
0
>>> 
>>> #interval=If set to 1, it will be incremented by 1.
>>>interval= 1
>>>Count()
1
>>>Count()
2
>>>Count()
3
>>> 

Other uses for functions defined within a function are "decorator" and "currying, partial application".

Recommended Posts

Difference between nonlocal and global in Python
Difference between list () and [] in Python
Difference between == and is in python
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and β€œ+” operator
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
[Python] Difference between class method and static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
File open function in Python3 (difference between open and codecs.open and speed comparison)
Practice applying functions and global variables in Python
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Find the difference in Python
Difference between regression and classification
[python] Calculation of months and years of difference in datetime
Stack and Queue in Python
Mutual conversion between JSON and YAML / TOML in Python
Difference between np.array and np.arange
Difference between MicroPython and CPython
Unittest and CI in Python
Difference in writing method to read external source code between Ruby and Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Difference between ps a and ps -a
Difference between return and print-Python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Get the current date and time in Python, considering the time difference
MIDI packages in Python midi and pretty_midi
Difference between SQLAlchemy filter () and filter_by ()
View photos in Python and html
Sorting algorithm and implementation in Python
Memorandum (difference between csv.reader and csv.dictreader)
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Cooperation between python module and API
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Check and move directories in Python
Difference between sort and sorted (memorial)