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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:04:15+00:00 2026-06-09T09:04:15+00:00

Okay, so I’ve had this really annoying problem where a variable got set locally

  • 0

Okay, so I’ve had this really annoying problem where a variable got set locally but then outside of that function reverted to it’s old self (in this case None), but at the same time I could manipulate other variables and still can without using the “global” keyword.

I can’t provide the real code for this but it goes something like this:

foo = {}
foo_foo = {}
bar = None


def changes_foo():
    ...do some stuff to foo...

class EditThread(threading.Thread):

    def __init__(self):
        setup()

    def run(self):
       for key, value in foo.items():
           do_update_task(key, value)

    def do_update_task(self, key, value):
         ...do some editing too foo...
         del foo[key]
         bar = [key, value]
         foo_foo[key] = value

def print_the_bar():
    print bar 

Please note that all the operations on foo and foo_foo works just fine, but bar is still None when I call print_the_bar, and I’ve had lots of print statements in my code to verify that bar inside of do_update_task indeed has the correct values and isn’t None.

Could someone please explain to me why it is so?

  • 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-09T09:04:17+00:00Added an answer on June 9, 2026 at 9:04 am

    When you do things to foo and foo_foo, you’re not changing the reference:

    foo = {}
    foo['key'] = 'stuff'
    

    foo still refers to the same object as before; it just now contains more data.

    bar = ['key', 'value']
    

    This reassigns bar to refer to a new object (the list with two elements).

    However, when that line is encountered inside a function, it creates a local reference bar unless you say global bar. In effect, you have two different variables named bar: the global and the local.

    Saying global bar tells Python to use the global version of bar rather than creating a new local variable with the same name.

    Generally, if you are modifying global variables, you should state global varname for each one to avoid accidentally creating a local.

    Or, you can use a class:

    class State(object):
        def __init__(self):
            self.foo = {}
            self.foo_foo = {}
            self.bar = None
    
    state = State()
    
    def fn():
        state.bar = ['key', 'value']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is a pretty weird question, but the situation is this. I've got
Okay, I feel a bit foolish for having to ask this but I guess
Okay, this is probably a very basic question; but, I'm just getting back in
Okay, so this is a long long shot but here it goes... When IE
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
Okay, so the answer to my question might not be the problem but here's
OKay, this is probably pretty noob, but I couldn't find how to solve it.
Okay, so I've known about this for like 600 years now, but I've only
Okay I got some good advice for Mobile Detection but still having an issue
Okay i have this problem with every page i make. im not sure what

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.