"Go" deep python:D ive into python Chinese version

Source: Internet
Author: User
Tags knowledge base

Original URL: http://woodpecker.org.cn/diveintopython/power_of_introspection/lambda_functions.html

4.7. UseLambdaFunction
    • 4.7.1. Lambda functions in the real world

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

Example 4.20.Lambdafunction Introduction
>>>  def< Span class= "Pyclass" > 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 accomplishes 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 omitted (implied, because the entire function has only one row). Moreover, the function does not have a function name, but it can be assigned to a variable for invocation.
You don't even need to assign a value to a variable when you use a lambda function. This may not be the most useful thing in the world, it just shows that the Lambda function is just an inline function.

In general, alambda function can receive any number of arguments (including optional parameters) and return the value of a single expression. A lambda function cannot contain commands and cannot contain more than one expression. Do not attempt to cram too much into the lambda function; If you need something more complicated, you should define a normal function, and then you want to make it long enough.

A lambda function is a stylistic problem. You don't have to use them; any place where you can use them, you can define a separate normal function to replace them. I use them to encapsulate special, non-reusable code, and avoid flooding my code with a large number of single-line functions.
4.7.1. In the real worldLambdaFunction

Lambda functions in apihelper.py :

    and (or (lambda s:s) 

Note that the simple form of the and-or technique is used here, and it is not a problem because the lambda function is always true in a Boolean environment. (This does not mean that the lambda function cannot return false values.) The Boolean value of this function object is true, and 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 with no parameters it is split by blank.

Example 4.21.SplitWith no parameters
>>>  s =  "this Is\na\ttest ">>>  print Sthis isatest>>>  print s.split () [' this ', ' is ', ' a ', ' Test ']>>>  print  ". Join (S.split ())  ' This is a test '            
This is a multiline string that replaces the triple quotation mark by using the definition of the escape character. \n   is a carriage return, \t   is a tab.
split   split by whitespace. So three spaces, a carriage return, and a tab are all the same.
split   Split string You can unify spaces, and then use a single space as a delimiter with  join   Reconnect it. This is the &NBSP;info   function adds multiple lines  doc string   Merge into a single line.

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

    and (or (lambda s:s) 

Processfunc is now a function, but which of these functions depends on the collapse variable. If collapse is true,processfunc(string) will compress the blanks; otherwise Processfunc (string) returns unchanged parameters.

To implement it in a less robust language, like Visual Basic, you would most likely want to create a function that takes a string argument and a collapse parameter, and uses the if statement to determine whether to compress whitespace, The corresponding value is then returned. This approach is inefficient because the function may need to handle every possible situation. Every time you call it, it will have to decide if you want to compress the blanks before giving them what you want. In Python, you can take the decision logic out of the function and define a cut-off Lambda function to provide the exact (unique) you want. This approach is more efficient, more elegant, and rarely leads to bugs that are annoying (oh, think those parameters are dizzy).

Lambdafunction 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 the future version of Python will change.) )
    • The Whole Python FAQ has an example of a vague use of Lambda single-line statements.

"Go" deep python:D ive into python Chinese version

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.