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.
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.
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.)
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.
it's a closure, but it's not an anonymous function; it has a name!
"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.
"anonymous function" is a term that refers to a syntax for declaring a function without immediately naming it. A function can't "become" anonymous at runtime by leaving the environment in which it was created.
"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.
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.
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.)