Hacker News new | ask | show | jobs
by julian37 5101 days ago
If you look closely, foo is an anonymous function

No, "foo" is a variable holding a reference to a named function--the function named "closure". You seem to be confused as to what an anonymous function is. (It doesn't per se have anything to do with closures.)

GP is right, Python's lambdas are crippled, unfortunately. And I'm saying that as someone who loves Python.

2 comments

At that point it is not a named function. The name was only associated with the function in a scope that is now exited.

Call the outer function 3 times. You'll wind up with 3 functions. They will be independent, even though you think they are all named the same thing.

There is literally nothing you could want to do with anonymous functions that you cannot do with this technique. (Of course if you want to update an enclosed variable, you have to store it in a mutable data structure. But that is true for any Python function that wants to mutate data in its surrounding environment.)

You are confusing closures and anonymous functions. The two are somewhat related, but not the same thing.

http://en.wikipedia.org/wiki/Anonymous_function

http://en.wikipedia.org/wiki/Closure_(computer_science)

There is no confusion here.

Once the function is passed out of the scope that it was created in, it becomes anonymous.

If you disagree, demonstrate how it is at that point associated with any available identifier.

This is getting a bit tiresome, but I'll play along since your high karma leads me to believe you're genuinely confused and not a troll.

The point of anonymous functions is not whether or not they will eventually be bound to an identifier, i.e. what happens with the function later on. The point is terseness in declaration: the ability to avoid having to define a full-blown function for small pieces of code that you want to pass to a higher-order function.

In Python, the difference between:

   def is_not_zero(x): return x != 0
   foo = bar.filter(is_not_zero)
and

   foo = bar.filter(lambda x: x != 0)
The Python language construct for anonymous functions is "lambda". Nothing more, nothing less. Closures don't have anything to do with it, except that of course both named functions and lambdas have closures created when they are referenced.

This is my last contribution to this thread. I'm just trying to help clear up an apparent misunderstanding on your part.

I hate pointless pedantry which is all that this discussion is.

Anyways anonymous function syntax is not always terse. So if terseness is the point, then it is a pointless distinction. Consider JavaScript. I am sure that you'd agree that the following is an anonymous function (Wikipedia certainly agrees that it is):

  function (x, y) {
    return x + y;
  }
But that is not very terse, is it? Even declaring a named function in Haskell is more terse than that by far. (And that anonymous function could just be: \x y -> x + y) But it is unambiguously an anonymous function syntax.

Anyways from the point of view of a programmer, what matters most is what you can say, and then secondly how convenient it is to say it. Anything that you would want to say with anonymous functions, you can say in Python. It will be more verbose by far than a real functional language like Haskell would be. But not really worse than many other scripting languages, like JavaScript.

But it's definitely worse than a scripting language like ruby.

Also, by this definition, the OP is just adding terseness to Java, not lambdas or anonymous functions.

You can unbind it by deleting it after using it so it is practically anonymous.