Home > Article > Backend Development > In-depth analysis of Python--Currying function
The example in this article describes an in-depth analysis of the Python--Currying function. Share it with everyone for your reference, the details are as follows:
1) It refers to changing the original function that accepts two parameters into a new one that accepts one parameter function process. The new function returns a function that takes the original second parameter as its parameter
2) Example: Currying the addition function: The method is to convert the function into a Currying function through nesting.
def add(x,y): return x + y def add(x): def _add(y): return x + y return _add
Requirements:
An addition function, I want to enhance its function and be able to output the called and called parameter information.
def add(x,y): return x + y
Now we add the information output function
def add(x,y): print('call add,x + y') #log output to the console return x + y
Although the above addition function fulfills the requirements, it has the following three shortcomings:
1. The coupling of the print statement is too high high.
2. The addition function belongs to the business function, but the function of outputting information can belong to the non-business function code and should not be placed in the business function addition.
Then we make the following improvements:
def add(x,y): return x + y def logger(fn): print('begin') #enhanced output x = fn(4,5) print('end') #enhanced features return x print(logger(add))
The above improvements achieve the separation of business functions, but passing parameters in the fn function call is a problem. We want to flexibly pass in parameters, but the parameters are in the internal function. How to use the flexible language python to solve this problem?
def add(x,y): return x + y def logger(fn): def _logger(*args,**kwargs): print('begin') c = fn(*args,**kwargs) print('end') return c return _logger logger(add)(2,3)
Now, we can enter parameters flexibly
A few more specific examples are given below:
def ad1(m,n,*,l=909): return m + n + l def loger(fn,*args,**kwargs): print('What\'s the matter?') ret = fn(*args,**kwargs) return ret print(loger(ad1,2,3,l=4)) #Can you currying them?
def add(x,y,*,z=9): return x + y + z def logger(fn,*args,**kwargs): print('This is a stupid function') ret = fn(*args,**kwargs) #after collections it needs to be deconstructed return ret print(logger(add,2,4,z=17))
def ad1(m,n,*,l=909): return m + n + l def loger(fn): def _loger(*args,**kwargs): #append a function as wrapper print('What\'s the matter?') ret = fn(*args,**kwargs) #the function which is be wrapped and transfer args and kwargs return ret return _loger #print(loger(ad1)(2,3,l=4)) also okay t = loger(ad1) #defind the function loger r = t(2,3,l=4) # income parameters and transfer it print(r)
Currying After learning, the most important and essential knowledge in python will be waiting for us - Decorator.
Related recommendations:
Currying in functional expressions
Scope, currying of JavaScript functions
The above is the detailed content of In-depth analysis of Python--Currying function. For more information, please follow other related articles on the PHP Chinese website!