Lambda anonymous function of Python learning

Source: Internet
Author: User

1

Python supports the runtime using "lambda" to establish an anonymous function (anonymous functions that is not bound to a name).

Python "Lambda" differs from the functional programming language, but he is very powerful and often comes with things like filter (), map (), reduce ()
and other classical concepts combined.

The following example normal and anonymous functions:

1 def return x**223print normalfun (8)4 5 6 Lambda x:x**278print anonymousfun (8)9 64

Both the normal function and the anonymous function are the same, but the anonymous function does not have a return statement, followed by a colon

An expression is the return value.

2 The following code snippet shows the anonymous function usage, make sure the Python version is above 2.2, because embedding scopes are required.

in [+]: def make_incrementor (n): return lambda x:x + nIn [121]: F = make_incrementor (2) in [122]: G = make_incrementor ( 6) in [123]: fout[123]: <function __main__.<lambda>>in [124]: gout[124]: <function __main__.<lambda >>in []: Print (42In) [126]: Print f (44In) [127]: Print g (42) 48

Note that g,f defines a post-type display bit <function __main__.<lambda>>, which shows that g,f is an anonymous function at this time.

3 following several codes demonstrate lambda and other binding usages

In [133]: foo = [2, 9, 8, 0, 27]in [134]: Print filter (lambda x:x% = = 9, foo) [1, 3, +, 27]in ]:? FilterType:        builtin_function_or_methodstring form: <built-in function filter>namespace:   Python Builtindocstring:filter (function or None, sequence), list, tuple, or stringreturn those items of sequence for which F Unction (item) is true.  Iffunction is None, and return the items is true.  If sequence is a tupleor string, return the same type, else return a list. In [136]: Print filter (foo)---------------------------------------------------------------------------TypeError                                 Traceback (most recent) <ipython-input-136-a3d764429161> in <module> ()----> 1 Print Filter (foo) Typeerror:filter expected 2 arguments, got 1In [137]: Print filter (None,foo) [2, 18, 9, 22, 17, 24, 8, 12, 27]

          ? maptype:        builtin_function_or_methodstring form: <built-in function map>namespace:   Python Builtindocstring:map (function, sequence[, sequence, ...]) Listreturn a list of the results of the function To the items ofthe argument sequence (s).  If more than one sequence are given, Thefunction is called with an argument list consisting of the Correspondingitem of EAC H sequence, substituting None for missing values when not allsequences has the same length.  

In [146]: Print reduce (lambda x,y:x + y, foo) 139In [147]:? Reducetype:        builtin_function_or_methodstring form: <bui lt-in function Reduce>namespace:   Python builtindocstring:reduce (function, sequence[, initial]) Valueapply a function of arguments cumulatively to the items of a sequence,from left to right, so as to reduce the SEQ Uence to a single value. For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]) calculates (((((1+2) +3) +4) +5).  If Initial is present, it's placed before the itemsof the sequence in the calculation, and serves as a default when these Quence is empty. In [148]: fooout[148]: [2, 18, 9, 22, 17, 24, 8, 12, 27]

Lambda anonymous function of Python learning

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.