Hacker News new | ask | show | jobs
by adrusi 5101 days ago
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.
1 comments

What name does it have AFTER it has been returned from the environment that it was created in?

If you have 5 different things that are all different but supposedly have the same name, what does it mean to have a name?

"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.