Sitemap
João Lima

João Lima

Anonymous functions are routines defined without an identifier associating them and are generally created to implement small functionality or to be used for a brief period (within the scope of a function, for example).

In Python, anonymous funciontions are defined from the lambda expression — for this reason they are also called “lambda functions” — and together with iterators and generators they form the basis of functional programming paradigm in this language.

Defining an anonymous function

An anonymous function, or lambda function, is defined with the following syntax.

anonymous = lambda: "Hi"

In this snippet, the variable “anonymous” is receiving a lambda function that’s returns “Hi”, with no “return” needed in the function, or the reserved word “def”.

Get João Lima’s stories in your inbox

Join Medium for free to get updates from this writer.

Its important to say, the anonymous function doesn’t necessarily have to be associated with a variable. You can write like this too:

(lambda: "Hi")

Actually is not a good programming practice named a lambda function, but if you want, you can!

Final Considerations

A lambda function is very useful in cases where we need a function to be passed as a parameter to another function, and that will no longer be needed after that, as if it were “disposable”.

So, if you want to read more about this, there some recommendations.

https://www.youtube.com/watch?v=xR57ED4DfFY&list=PLOQgLBuj2-3LRIKxqcse1EL4hXhUFuHsR

--

--