Lambda functions:
Syntax:
lambda argument(s): expression
eg.,
A trivial example.
def areaofcircle(radius): return math.pi * radius * radius
can be written as:
circlearea = lambda radius : math.pi * radius * radius
In this example, the lambda function accepts a lone argument radius; and the function evaluates the lambda expression (π * radius2) to calculate the area of a circle and returns the result to caller.
In case of lambda function, the identifier "circlearea" is assigned the function object the lambda expression creates so circlearea can be called like any normal function. eg., circlearea(5)
Another trivial example that converts first character in each word in a list to uppercase character.
>> fullname = ["john doe", "richard roe", "janie q"] >>> fullname ['john doe', 'richard roe', 'janie q'] >>> map(lambda name : name.title(), fullname) ['John Doe', 'Richard Roe', 'Janie Q']
Alternative URL: Python Lambda Functions @technopark02.blogspot.com