def return_closure (value):
def closure ():
return value
return closure
foo = return_closure(5)
print foo() # prints 5
If you look closely, foo is an anonymous function, and it could have anything you bloody well please inside of it.What you can't do is put statements in an anonymous function that you are declaring inline. But the full syntax I just demonstrated is only marginally longer.
UPDATE
Modify the end like this to see that the functions really are anonymous.
foo = return_closure(1)
bar = return_closure(2)
baz = return_closure(3)
print foo()
print bar()
print baz()
You will see that the three functions returned are completely independent. They are not associated with any name or each other. They are, in fact, anonymous. (Admittedly at the moment they are called foo, bar and baz. But you could have put them into an array, and they really would have no unique name.)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.
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.)
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.
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.
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.
Also, by this definition, the OP is just adding terseness to Java, not lambdas or anonymous functions.
"anonymous function" is a syntactic feature but "lexical closure" is a language feature. In python "anonymous functions" can comprise only one expression: `lambda x: x * 2`, but that's not the only way to make a lexical closure, you can also do:
def foo(x):
return x * 2
and then use `foo` in place of the anonymous function literal.If you have 5 different things that are all different but supposedly have the same name, what does it mean to have a name?
"lexical closure" is a term used to describe the way that functions behave at runtime. A lexical closure can be used as a first class value, and retains the scope that it was created in, as opposed to adopting the scope in which is is called. Example:
def foo(bar):
a = 2
bar()
a = 5
def baz():
print a
will print "5" not "2". but note that `baz` is not anonymous because being anonymous is a syntactic quality and in the source code, `baz` is named in the same operation as it is declared.