Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In python, it would be proper to say that ALL functions are lambdas except that some/most of those lambdas are assigned to variables via syntactic sugar (this is made very plain in sicp which is the main reason for suggesting it).The only thing different about python vs most other languages is guido refusing to implement multi-line lambdas.

As to any complaint, lambdas exist in python (that is, are made accessible to the programmer) for the exact reason python also has a for loop in addition to a while loop -- because they make things easier and more maintainable.

  def myApplyAdd(func, x):
    return lambda y: func(x+y)

  applyAddInst = myApplyAdd(lambda z: z*2, 20)
  applyAddInst(3) #output 26
becomes much less maintainable without lambdas and pollutes the scope with unnecessary variables you must then keep track of

  def myApplyAdd(func, x):
    def innerAdd (y):
      return func(x + y)
    return innerAdd

  def mulByTwo(x):
    return x*2

  applyAddInstance = myApplyAdd(mulByTwo, 20)
  applyAddInst(3) #output 26
I would also note that list comprehensions make heavy use of lambdas. For example, the following is actually using a for loop and calling

  lambda i: i**2
each each iteration

  result = [ i**2 for i in range(3, 30) ]


"In python, it would be proper to say that ALL functions are lambdas except that some/most of those lambdas are assigned to variables via syntactic sugar"

If a function is "assigned to a variable" then it's no longer a lambda, because it's bound to an identifier. But I suppose the lines are a tad blurred.


While I agree that the function "has a name", the function is still anonymous in the sense that pointers don't have types, only values have types. As a side effect of converting church's model to Turing's model, all "unnamed" lambda functions are given one-time symbols as are all anonymous numbers, arrays, classes, objects, etc.

  x = lambda x: x+x #a lambda, but has a named pointer
  x = 5
lambda is still in memory (until garbage collection), but it no longer has a pointer to it.

  def x(y):
    return y+y
  
  x = 5 #the "named" function above is now without a pointer
A little off-topic (but answers your previous question and perhaps looks a little deeper into lambdas), but let's take a quick look at javascript. JS developers use lambda functions both because lexical closures don't pollute the global namespace, they offer modules and alternatives to prototypal objects, and because the language tends to make heavy use of callbacks (which demonstrate the bad side of lambda functions). JS also only makes subtle differences between lambdas and named functions.

  //lambda expression
  var x = function (){};

  //named function
  function x() {}

  //named lambda expression
  var x = function x() {};

  //self-executing lambda (returns 5)
  (function (x) {return x;}(5));
note that this would be a self-executing lambda in python

  (lambda x: x)(5)
basic module pattern using self-executing lambda

  my_module = (function (self) {
    "use strict"
    self = self || {};

    //private
    //pretty much inaccessible to the outside scope
    var privateVar = 5;
    //lambda expression again
    var privateFunc = function () {};

    //public
    self.publicVar = 5;
    self.publicFunc = function () {};
    self.getPrivateVar = function () {
      return privateVar;
    };

    return self;
  }(my_module));

  my_module.publicVar = 8;
  my_module.publicFunc();
  my_module.getPrivateVar();
This is where too many lambdas becomes a problem. Judicious use of naming (even though they're only used once) makes things more legible.

  func1(function () {
    func2(function () {
      func3(function () {
        func4(function () {
      })
    })
  })




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: