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 8184971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:41:10+00:00 2026-06-07T01:41:10+00:00

Today I explored a weird behavior of Python. An example: closures = [] for

  • 0

Today I explored a weird behavior of Python. An example:

closures = []
for x in [1, 2, 3]:
    # store `x' in a "new" local variable
    var = x

    # store a closure which returns the value of `var'
    closures.append(lambda: var)

for c in closures:
    print(c())

The above code prints

3
3
3

But I want it to print

1
2
3

I explain this behavior for myself that var is always the same local variable (and python does not create a new one like in other languages). How can I fix the above code, so that each closure will return another value?

  • 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-07T01:41:11+00:00Added an answer on June 7, 2026 at 1:41 am

    The easiest way to do this is to use a default argument for your lambda, this way the current value of x is bound as the default argument of the function, instead of var being looked up in a containing scope on each call:

    closures = []
    for x in [1, 2, 3]:
        closures.append(lambda var=x: var)
    
    for c in closures:
        print(c())
    

    Alternatively you can create a closure (what you have is not a closure, since each function is created in the global scope):

    make_closure = lambda var: lambda: var
    closures = []
    for x in [1, 2, 3]:
        closures.append(make_closure(x))
    
    for c in closures:
        print(c())
    

    make_closure() could also be written like this, which may make it more readable:

    def make_closure(var):
        return lambda: var
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I explored something weird: jQuery Address do not work in jQuery callbacks: http://jsfiddle.net/Wh7uS/19/
Today, I installed Visual Studio 2010 Professional on a new laptop with a fresh
Using OneNote 2010 doing some C# interop stuff. Observing some very strange behavior, which
I am new to .net and I have downloaded VS 2008 trail version today.
<script language=JavaScript> <!-- Hide from older browser var x= new Date() var y= x.getYear()
I'm experiencing something very unusual in my VS2010 today which it hasn't done in
Today I was working on a project in which I wanted to alias an
today i hit a really annoing problem with wpf. i just want to align
Today at work, I was given a task to fix a bug within our
Today I have had a problem with hiding text with text-indent: -9999px rule. I

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.