Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:56:20+00:00 2026-06-14T06:56:20+00:00

I am trying to use closures to eliminate a variable from a function signature

  • 0

I am trying to use closures to eliminate a variable from a function signature (the application is to make writing all the functions needed for connecting Qt signals for an interface to control a largish number of parameters to the dictionary that stores the values ).

I do not understand why the case of using the lambda not wrapped in another function returns the last name for all cases.

names = ['a', 'b', 'c']

def test_fun(name, x):
    print(name, x)

def gen_clousure(name):
    return lambda x: test_fun(name, x)

funcs1 = [gen_clousure(n) for n in names]
funcs2 = [lambda x: test_fun(n, x) for n in names]

# this is what I want
In [88]: for f in funcs1:
   ....:     f(1)
a 1
b 1
c 1

# I do not understand why I get this
In [89]: for f in funcs2:
   ....:     f(1)
c 1
c 1
c 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:56:21+00:00Added an answer on June 14, 2026 at 6:56 am

    The reason is that closures (lambdas or otherwise) close over names, not values. When you define lambda x: test_fun(n, x), the n is not evaluated, because it is inside the function. It is evaluated when the function is called, at which time the value that is there is the last value from the loop.

    You say at the beginning that you want to “use closures to eliminate a variable from a function signature”, but it doesn’t really work that way. (See below, though, for a way that may satisfy you, depending on what you mean by “eliminate”.) Variables inside the function body will not be evaluated when the function is defined. In order to get the function to take a “snapshot” of the variable as it exists at function-definition time, you must pass the variable as an argument. The usual way to do this is to give the function an argument whose default value is the variable from the outer scope. Look at the difference between these two examples:

    >>> stuff = [lambda x: n+x for n in [1, 2, 3]]
    >>> for f in stuff:
    ...     print f(1)
    4
    4
    4
    >>> stuff = [lambda x, n=n: n+x for n in [1, 2, 3]]
    >>> for f in stuff:
    ...     print f(1)
    2
    3
    4
    

    In the second example, passing n as an argument to the function “locks in” the current value of n to that function. You have to do something like this if you want to lock in the value in this way. (If it didn’t work this way, things like global variables wouldn’t work at all; it’s essential that free variables be looked up at the time of use.)

    Note that nothing about this behavior is specific to lambdas. The same scoping rules are in effect if you use def to define a function that references variables from the enclosing scope.

    If you really want to, you can avoid adding the extra argument to your returned function, but to do so you must wrap that function in yet another function, like so:

    >>> def makeFunc(n):
    ...     return lambda x: x+n
    >>> stuff = [makeFunc(n) for n in [1, 2, 3]]
    >>> for f in stuff:
    ...     print f(1)
    2
    3
    4
    

    Here, the inner lambda still looks up the value of n when it is called. But the n it refers to is no longer a global variable but a local variable inside the enclosing function makeFunc. A new value of this local variable is created every time makeFunc is called, and the returned lambda creates a closure that “saves” the local variable value that was in effect for that invocation of makeFunc. Thus each function created in the loop has its own “private” variable called x. (For this simple case, this can also be done using a lambda for the outer function — stuff = [(lambda n: lambda x: x+n)(n) for n in [1, 2, 3]] — but this is less readable.)

    Notice that you still have to pass your n as an argument, it’s just that, by doing it this way, you don’t pass it as an argument to the same function that winds up going into the stuff list; instead you pass it as an argument to a helper function that creates the function you want to put into stuff. The advantage of using this two-function approach is that the returned function is “clean” and doesn’t have the extra argument; this could be useful if you were wrapping functions that accepted a lot of arguments, in which case it could become confusing to remember where the n argument was in the list. The disadvantage is that, doing it this way, the process of making the functions is more complicated, since you need another enclosing function.

    The upshot is that there is a tradeoff: you can make the function-creation process simpler (i.e., no need for two nested functions), but then you must make the resulting function a bit more complicated (i.e., it has this extra n=n argument). Or you can make the function simpler (i.e., it has no n=n argument), but then you must make the function-creation process more complicated (i.e., you need two nested functions to implement the mechanism).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use Powershell to script the removal of specific custom errors from an
In trying to learn JavaScript closures, I've confused myself a bit. From what I've
Javascript client side application. Trying to eliminate memory leaks leads to ugly (to say
I am trying to use the BGGA closures prototype with an existing JDK 6
I'm trying to make this code nicer by using scala closures: SQLiteQueue queue =
I am trying to use a closure to ensure that a function can only
I am trying to set a variable value at an object (class) level from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.