Python function parameter transfer in stages
Recently I used a project. Considering the efficiency, I need to use the filter () function in Python to filter a list. This filter function actually requires two parameters while filter () only one parameter can be passed to the filter function. After careful analysis, one of the two parameters remains unchanged during the request lifetime, and the other is the value used for filtering. So I finally used Lambda to implement the phased parameter transfer of a function. Example:
Filter function:
Def myfilter (A, B ):
"" B is a parameter that can't change at a request """
If a % 2 = 0:
Return true
Else:
Return false
Execution Process
Fc = Lambda A: myfilter (A, 20)
Filter (FC, [1, 2, 3, 4, 5, 6, 7, 8])
In this way, the two parameters of the filter function myfilter () are passed separately.
Python is a dynamic language and is often called the slowest in a dynamic language. The recent appearance of Ruby has slightly changed this situation. I have heard that Ruby is more "dynamic" than python. An alternative way to improve efficiency in python is to avoid using dynamic features while ensuring readability. For example, too many "." operators are inefficient. In addition, some functional programming methods can be used, such as many built-in functions, such as map (), filter (), zip () and so on. Using Lambda is also a good method.