Python learning--using lambda functions

Source: Internet
Author: User
Tags knowledge base

Python supports an interesting syntax that allows you to quickly define the smallest function of a single line. These functions, called Lambda, are borrowed from Lisp and can be used wherever a function is needed. example 4.20. Lambda function Introduction

>>> def f (x):
...     Return x*2 ...     
>>> F (3)
6
>>> g = lambda x:x*2  >>> g (3)
6
>>> (lambda x:x*2) ( 3) # 6
This is a lambda function that completes the same thing as the normal function above. Note the short syntax here: there is no parentheses around the argument list, and the return keyword is ignored (implied because there is only one row for the entire function). Also, the function does not have a function name, but it can be assigned to a variable for invocation.
When you use a lambda function, you do not even need to assign it to a variable. This is probably not the most useful thing in the world, it just shows that the lambda function is just an inline function.

In general, a lambda function can receive any number of arguments, including optional arguments, and returns the value of a single expression. A lambda function cannot contain a command and cannot contain more than one expression. Don't try to cram too much into a lambda function; If you need something more complex, you should define a normal function and how long you want it to be.

Lambda functions are a matter of style. You don't have to use them, and any place where you can use them can define a single normal function to replace it. I use them on the need to encapsulate special, reusable code to avoid flooding my code with a lot of single-line functions.
4.7.1. Lambda functions in the real world

Lambda functions in apihelper.py:

    Processfunc = Collapse and (lambda s: "". Join (S.split ())) or (lambda s:s)

Note that the simple form of the and-or technique is used here, because lambda functions are always true in Boolean environments. (This does not mean that the lambda function cannot return a false value.) The Boolean value of this function object is true; its return value can be anything. )

Also note that the split function with no parameters is used. You've seen it with one or two parameters, but without parameters it is segmented by whitespace. example 4.21. Split without parameters

>>> s = "This   is\na\ttest"  >>> print S. is
a	test
>>> Print s.split ()           [' This ', ' are ', ' a ', ' test ']
>>> print '. Join (S.split ()) ' ' is ' a test '
This is a multiline string that replaces triple quotes by using the definition of an escape character. \ n is a carriage return, \ t is a tab character.
Split with no parameters is separated by blanks. So three spaces, a carriage return, and a tab are all the same.
You can unify a space by splitting the string, and then reconnect it with a single space as a delimiter. This is what the info function does to combine multiple lines of doc string into a single line.

So what does the info function do with these lambda functions, the Split function, and the and-or technique?

    Processfunc = Collapse and (lambda s: "". Join (S.split ())) or (lambda s:s)

Processfunc is now a function, but which function depends on the collapse variable. If collapse is true, Processfunc (string) compresses white space, or Processfunc (string) returns the unchanged argument.

Implement it in a less robust language, like  visual Basic, you might want to create a function that accepts a string parameter and a  collapse  parameter, and uses  if  Statement to determine whether to compress white space, and then return the corresponding value. This approach is inefficient because the function may need to deal with every possible situation. Every time you call it, it will have to decide whether to compress the blanks before giving you what you want. In  Python , you can take decision logic out of the function, and define a cut  lambda  function to provide the exact (unique) you want. This approach is more efficient, more elegant, and rarely causes bugs that are annoying (oh, thinking about those parameters). lambda  function for further reading Python knowledge base  discusses the use of  lambda  to invoke functions indirectly. python tutorial  demonstrates how to access external variables from within a  lambda  function. (pep 227  explains how it will change in the future version of  Python .) The whole python faq  has an example of a vague use  lambda  single-line statement. from:http://woodpecker.org.cn/diveintopython/power_of_introspection/lambda_functions.html

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.