Python: Using Lambda




Introduction


Anonymous functions are functions that have no name.
Because these functions do not have names, where they would put their name, there are direct instructions defining the function introduced by a special syntax.

In functional programming languages, functions anonymous are often called lambda functions, referring to the lambda calculus, theoretical tool program in which all form of function call, even though no basic function is defined. [Wikipédia]


Lambda in Python



Lambda may be used as:

  • A method. It can store / return variable (string, integer, etc. ..).

  • A keyword in a high level programming (such is the case of Python). However, in Python, we consider that right Lambda can not return a single expression.

Example based in python3:


 def get (f, n):print (f (n))
# Create two lambdassquare = lambda x: x * xcube = lambda x: x * x * x
# lambda Skip todo (square, 4)do (cube, 3)

No comments:

Powered by Blogger.