Python function parameter transfer in stages

Source: Internet
Author: User
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.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.