What is the use of python lambda functions

These are simple useful oneliner mini functions that take in multiple parameters and return a single value. Main benefits

  • These can be defined as and when we code
  • Doesn’t require a function name, thus helps in reserving function names for meaningful functions/stubs
  • any 2 liner repeatable code should ideally be converted into lambda functions
  • eg
x = lambda a : a + 10
print(x(5)) 
returns 15

The equivalent of it in JavaScript is

(num) => {
  return num * 2
}

Thumb rule is to limit its usage for anything that can be written 2-3 lines, for anything beyond it or that has more logic into it, better to write functions.

Leave a Reply

Close Menu
Follow by Email
Facebook
Google+
Twitter
LinkedIn