Usage of Python lambda

Source: Internet
Author: User

Welcome reprint, Reproduced please specify the original address: http://blog.csdn.net/majianfei1023/article/details/45269343


A lambda function is also called an anonymous function, and the function does not have a specific name. Let's take a look at one of the simplest examples:


def f (x): return x * 2g = Lambda x:x * 2

look at how f and g are different, f is defining a function, passing in a parameter x, then x*2,g is defining a lambda, followed by an X (this is the argument), followed by a: X * 2 (this is the operation performed), so the above f = g. They are exactly the same thing that you can write: F (10), or so write: g (10).

But what is the meaning of lambda existence? In fact, simply put, lambda expressions are often used in situations where a function is required, but you do not want to bother to name a function.

For example, there are several built-in functions in Python, like map,filter,reduce, and I assume you understand their usage.

A simple programming problem: print a multiple of 3 within 100.

You might write:

def func (): result = []for i in xrange: if I% 3 = = 0:result.append (i) return result

or if you know more about Python's features, you can filter by using filter:

def func (x): return x%3 = = 0filter (func,[i for I in Xrange (100)])

In any case, we need to define a function, or evaluate, or filter, can not be so troublesome, so simple programming problem, every time to define a function, very troublesome, Lambda should play,

Filter (lambda x:x%3==0,[i for I in Xrange (100)])

that's the way it's done. Note that this lambda, with the above func, does exactly the same thing, except that it does not have to be shown to define it.


After the basics, let's look at how we use lambda in the project:

We need to look up a player's data (Find_user_info), where it might be necessary to do something very complicated, such as querying the Database (Query_database), and then logically manipulating the results (Deal_data):

def deal_data (Result,fid):  # This is where we deal with the main logic, possibly adding its properties, reducing its properties, or deleting it. ... def query_database (UID): result = ... return resultdef find_user_info (UID): result = Query_database (UID) deal_data ( RESULT,UID)

In General, Find_user_info and Query_database are common interfaces, we do not change it very much, we have to do is to change the operation of different deal_data. For example, I want to change its name, or, I want to add 100 gold coins, how to deal with it, the parameters are not enough, that good, in Find_user_info () add a type, in the deal_data inside add a variety of conditions to judge, jump to each specific processing function, a look is not good code.

We can do more general processing with Lambda:

def modify_name (Result,fid): ... def add_gold (Result,fid): ... def query_database (UID): result = ... return resultdef find_ User_info (uid,callback): result = Query_database (UID) callback (Result) Find_user_info (Uid,lambda result:deal_data ( RESULT,FID))

Modify the parameters of the Find_user_info, add a call_back, and then we call the time this call_back is a lambda, we want to rename, Deal_data replaced Modify_name, we want to add gold, That will be changed to Add_gold, as long as the parameters meet the requirements can be rich to modify the Find_user_info, but also to separate our different processing logic.


Usage of Python lambda

Related Article

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.