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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:17:12+00:00 2026-05-24T03:17:12+00:00

Have a look at this code: def closure(): value = False def method_1(): value

  • 0

Have a look at this code:

def closure():
    value = False

    def method_1():
        value = True

    def method_2():
        print 'value is:', value

    method_1()
    method_2()

closure()

I would expect it to print ‘Value is: True’ but it doesn’t. Why is this and what is the solution?

  • 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-24T03:17:14+00:00Added an answer on May 24, 2026 at 3:17 am

    This happens because method_1 gets its own local scope where it can declare variables. Python sees value = True and thinks you’re creating a new variable named value, local to method_1.

    The reason Python does this is to avoid polluting the outer scope’s locals with variables from an inner function. (You wouldn’t want assignments in regular, module-level functions to result in global variables being created!)

    If you don’t assign to value, then Python searches the outer scopes looking for the variable (so reading the variable works as expected, as demonstrated by your method_2).

    One way to get around this is by using a mutable object instead of assigment:

    result = { 'value': False }
    
    def method_1():
        result['value'] = True
    

    In Python 3, the nonlocal statement (see also docs) was added for exactly this scenario:

    def method_1():
        nonlocal value
        value = True    # Works as expected -- assigns to `value` from outer scope
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at the following code: class A(object): defaults = {'a': 1} def
I have code for a Range class like this: class Range: def __init__(self, start,
How would i go about this? I tried Net::HTTP.new(@site).head('/').kind_of? Net::HTTPOK but I receive this:
I haven't been able to find anything about this value error online and I
I have two numpy arrays of different shapes, but with the same length (leading
I have a controller called ProjectsController . Its actions, by default, look for views
I have some problem with sequence generator. I have a file where each line
I'm trying to show only 5 reviews from a specific category as opposed to
I am implementing a datastructure and want the user to be able to use
So I'm stuck on a newbie problem once more :D I'm trying to mash

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.