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

The Archive Base Latest Questions

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

Below is an example I got from someone’s blog about python closure. I run

  • 0

Below is an example I got from someone’s blog about python closure.
I run it in python 2.7 and get a output different from my expect.

flist = []

for i in xrange(3):
    def func(x):
        return x*i
    flist.append(func)

for f in flist:
    print f(2)

My expected output is: 0, 2, 4
But the output is: 4, 4, 4
Is there anyone could help to explain it?
Thank you in advance.

  • 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-07T09:10:49+00:00Added an answer on June 7, 2026 at 9:10 am

    Loops do not introduce scope in Python, so all three functions close over the same i variable, and will refer to its final value after the loop finishes, which is 2.

    It seems as though nearly everyone I talk to who uses closures in Python has been bitten by this. The corollary is that the outer function can change i but the inner function cannot (since that would make i a local instead of a closure based on Python’s syntactic rules).

    There are two ways to address this:

    # avoid closures and use default args which copy on function definition
    for i in xrange(3):
        def func(x, i=i):
            return x*i
        flist.append(func)
    
    # or introduce an extra scope to close the value you want to keep around:
    for i in xrange(3):
        def makefunc(i):
            def func(x):
                return x*i
            return func
        flist.append(makefunc(i))
    
    # the second can be simplified to use a single makefunc():
    def makefunc(i):
        def func(x):
            return x*i
        return func
    for i in xrange(3):
        flist.append(makefunc(i))
    
    # if your inner function is simple enough, lambda works as well for either option:
    for i in xrange(3):
        flist.append(lambda x, i=i: x*i)
    
    def makefunc(i):
        return lambda x: x*i
    for i in xrange(3):
        flist.append(makefunc(i))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get only unique departments from the below example? Dept Id Created
The example below is from a REST database driver on Python 2.7. In the
The below example is got from Jon Skeet's article, Parameter passing in C# .
I am using the batch file example below that I got from batch file
Below is the example I got from google finance for a stock quote. But
I modified an example with threads and got the example with multiprocessing included below.
I have got a memory leak problem in the example below(u can download the
I'm testing the VB function below that I got from a Google search. I
Let's consider the below example. There, I have: target MAIN calls target t and
Reproducible example below. I have a simulation loop, within which I occasionally have rows

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.