Please tell me about higher-order functions in Python.
I couldn't imagine the order of processing the next higher-order function sample code.
def log_func (func):
def inner (* args, ** keywds):
print ('-----------------')
print (f'Name: {func.__name__}')
print (f'Args: {args}')
print (f'Keywds: {keywds}')
print ('-----------------')
return func (* args, ** keywds)
return inner
def hoge (x, y, m ='bar', n ='piyo'):
print (f'hoge: {x}-{y}/{m}-{n}')
log_hoge = log_func (hoge)
log_hoge (15, 37, m ='hoge', n ='piyo')
So, I put print in various places as follows and tried to process it.
def log_func (func):
print ("A")
def inner (* args, ** keywds):
print ("B")
print ('-----------------')
print (f'Name: {func.__name__}')
print (f'Args: {args}')
print (f'Keywds: {keywds}')
print ('-----------------')
return func (* args, ** keywds)
print ("C")
return inner
def hoge (x, y, m ='bar', n ='piyo'):
print ("D")
print (f'hoge: {x}-{y}/{m}-{n}')
print ("E")
log_hoge = log_func (hoge)
print ("F")
log_hoge (15, 37, m ='hoge', n ='piyo')
This output result is
E
A
C
F
B
-----------------
Name: hoge
Args: (15, 37)
Keywds: {'m':'hoge','n':'piyo'}
-----------------
D
hoge: 15-37/Hoge-Piyo
have become.
I have a question here.
For myself
E → A → C → B → D → F
I was expecting it to be, but it didn't seem to be the case.
In short, since the inner function is called with return immediately after C, I thought that after C, I would go to B inside the inner function.
Could you please tell me why you fly to point F instead of the inner function after C?
-
Answer # 1
-
Answer # 2
Immediately after C, the inner function is called with return, so I was wondering if C would go to B inside the inner function.
return inner
Does not "execute" the inner function. It's just returning the definition.
B is displayed when the inner function is "executed", that is, after F when the inner function as the variable log_hoge is executed.
Related articles
- python about iterative processing with specified numbers
- python - i want to connect the images in two folders in order
- python - about multiple processing and loop processing in discordpy
- python - about the output order of set {}
- parallel processing using python multiprocessingpool and multiprocessingqueue does not work well
- python - how to get alphabets in order with for in
- python:about processing such as timesleep and wxpython
- i was given a python 3 assignment as a cram school assignment, but i don't understand "functions and comprehensions, while
- python - avoid processing when duplicated
- python iterative processing num is not defend
- python - while syntax processing
- i want to speed up the processing of [python] for
- processing using the len function when an integer value is obtained from python standard input
- i want to create a function in python that all functions without class go through
- javascript - about the processing order of if statements
- javascript - about the order of push processing
- i want to add processing to the python library
- python - about range and int type processing
- python 3x - update processing and multi-process with pyqtgraph
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Understand that the definition of a function and the execution of a function are different.
The next return inner of print ("C") does not execute the inner, it just returns the defined function inner. Try the following:
So far, we've just received the defined function inner and assigned it to log_hoge, so inner hasn't been executed yet.