Tuesday, October 27, 2015

lambda, filter, reduce and map

http://www.python-course.eu/lambda.php

http://www.secnetix.de/olli/Python/lambda_functions.hawk


Note: using lambda, a python function can be defined without def and there is no need to use return in the function. Also a anonymous function - a function without name.

=> No need to use def and return for creating a function and returning a value.
=> function define without def & return value without return
=> define anonymous function: a function without a name
=> one liner function definition
=> returning a function from other function

>>> def a(x): return lambda y: x+y
...
>>> b=a(2)
>>> b(3)
5



Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".

>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2
>>>
>>> print g(8)
64


No comments:

Post a Comment