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

  • Home
  • SEARCH
  • 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 8398479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:03:57+00:00 2026-06-09T21:03:57+00:00

In the 2nd case below, Python tries to look for a local variable. When

  • 0

In the 2nd case below, Python tries to look for a local variable. When it doesn’t find one, why can’t it look in the outer scope like it does for the 1st case?

This looks for x in the local scope, then outer scope:

def f1():
    x = 5
    def f2():
         print x

This gives local variable 'x' referenced before assignment error:

def f1():
    x = 5
    def f2():
        x+=1

I am not allowed to modify the signature of function f2() so I can not pass and return values of x. However, I do need a way to modify x. Is there a way to explicitly tell Python to look for a variable name in the outer scope (something similar to the global keyword)?

Python version: 2.7

  • 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-09T21:03:58+00:00Added an answer on June 9, 2026 at 9:03 pm
    def f1():
        x = { 'value': 5 }
        def f2():
            x['value'] += 1
    

    Workaround is to use a mutable object and update members of that object. Name binding is tricky in Python, sometimes.

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

Sidebar

Related Questions

In the below example how do i mention speed in 2nd case or atleast
how can declare a 2nd or multidimensional array without first size in c++? class
How can i append 2nd page of a pdf file to the first page.
The typical header should be #!/usr/bin/env python But I found below also works when
so I have two arrays. one of them looks like this (it's values or
**1.**for 2nd level cache, can only set timeout period but cannot force refresh/clear cache
I have a simple click function with the code below, but I can't seem
below is a code example and the question, please note that I can NOT
In the function below, I want to match the keyword case insensitive (should match
For the 2nd <h3>World!</h3> in this sample scenario ( Please note <..> elements are

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.