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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:55:21+00:00 2026-05-16T01:55:21+00:00

I started working in Python just recently and haven’t fully learned all the nuts

  • 0

I started working in Python just recently and haven’t fully learned all the nuts and bolts of it, but recently I came across this post that explains why python has closures, in there, there is a sample code that goes like this:

y = 0

def foo():
    x = [0]
    def bar():
        print x[0], y
    def change(z):
        global y
        x[0] = y = z

    change(1)
    bar()
    change(2)
    bar()
    change(3)
    bar()
    change(4)
    bar()

foo()

1 1
2 2
3 3

and basically I don’t understand how it actually works, and what construct like x[0] does in this case, or actually I understand what it’s doing, I just don’t get how is it this 🙂

  • 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-05-16T01:55:21+00:00Added an answer on May 16, 2026 at 1:55 am

    Before the nonlocal keyword was added in Python 3 (and still today, if you’re stuck on 2.* for whatever reason), a nested function just couldn’t rebind a local barename of its outer function — because, normally, an assignment statement to a barename, such as x = 23, means that x is a local name for the function containing that statement. global exists (and has existed for a long time) to allow assignments to bind or rebind module-level barenames — but nothing (except for nonlocal in Python 3, as I said) to allow assignments to bind or rebind names in the outer function.

    The solution is of course very simple: since you cannot bind or rebind such a barename, use instead a name that is not bare — an indexing or an attribute of some object named in the outer function. Of course, said object must be of a type that lets you rebind an indexing (e.g., a list), or one that lets you bind or rebind an attribute (e.g., a function), and a list is normally the simplest and most direct approach for this. x is exactly that list in this code sample — it exists only in order to let nested function change rebind x[0].

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

Sidebar

Related Questions

I've just started working with Propel and I love it, but I have a
I just started working on Python and have been trying to run an outside
I've just started to learn the long-heard python language. I've been working with C
I've just started working with the basics of python socket networking. As an exercise
I'm fairly new to Python, and I've just started working with XML parsing. I
So I'm just getting started with Python, and currently working my way through http://diveintopython3.ep.io/
I'm used to working with Sphinx for C++ and Python projects. I've just started
I started working on a little Python script for FTP recently. To start off
I just started studying Python two days ago, so my code is sloppy...But I'm
I started working on CodeIgniter recently and I'm having some issues with the .htaccess

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.