Python Anonymous Function
Jump to navigation
Jump to search
A Python Anonymous Function is a Python function that is an anonymous function.
- AKA: Python Lambda/Unnamed Function.
- See: Python Expression, Python Function Definition, AWS Lambda Function.
References
2014
- https://docs.python.org/2/reference/expressions.html#lambda
- Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression
lambda arguments: expression
yields a function object. The unnamed object behaves like a function object defined with
def name(arguments):
return expression
- Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression
2013
- http://www.secnetix.de/olli/Python/lambda_functions.hawk
- Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda". This is not exactly the same as lambda in functional programming languages, but it is a very powerful concept that's well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce().
This piece of code shows the difference between a normal function definition ("f") and a lambda function ("g"):
- Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda". This is not exactly the same as lambda in functional programming languages, but it is a very powerful concept that's well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce().
>>> def f (x): return x**2 … >>> print f(8) 64
>>> g = lambda x: x**2 ... >>> print g(8) 64
- http://www.secnetix.de/olli/Python/lambda_functions.hawk
- As you can see, f() and g() do exactly the same and can be used in the same ways. Note that the lambda definition does not include a "return" statement -- it always contains an expression which is returned. Also note that you can put a lambda definition anywhere a function is expected, and you don't have to assign it to a variable at all.
2012
- http://docs.python.org/2/glossary.html#term-lambda
- An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [arguments]: expression