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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:08+00:00 2026-05-27T01:46:08+00:00

Wow, this seems so basic, but I can’t get it to work. All I

  • 0

Wow, this seems so basic, but I can’t get it to work. All I need to do is store a global dict which can be accessed and modified from other modules & threads.

What’s the “best practices” way of achieving this?

test.py

import testmodule

class MyClassA():
    def __init__(self, id):
        self.id = id

if __name__ == '__main__':
    global classa_dict
    classa_dict = {}
    classa_dict[1] = MyClassA(1)
    classa_dict[2] = MyClassA(2)

    testing = testmodule.TestModule()

testmodule.py

class TestModule():
    def __init__(self):
        global classa_dict
        print classa_dict[2]

output

$ python test.py 
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    testing = testmodule.TestModule()
  File "/path/to/project/testmodule.py", line 4, in __init__
    print classa_dict[2]
NameError: global name 'classa_dict' is not defined
  • 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-27T01:46:09+00:00Added an answer on May 27, 2026 at 1:46 am

    You can in fact acchieve something like what you want:

    testmodule.py:

    classa_dict={}
    
    class Test():
        def __init__(self):
            global classa_dict
            print classa_dict[2]
    

    test.py:

    import testmodule
    
    class MyClassA():
        def __init__(self, id):
            self.id = id
    
    if __name__ == '__main__':
        lclassa_dict = testmodule.classa_dict
        lclassa_dict[1] = MyClassA(1)
        lclassa_dict[2] = MyClassA(2)
    
        testing = testmodule.Test()
    

    Please note that global is used somewhat else than what you might think of. If you write global varname, this means, that Python should not generate a local variable varname, but look in the global scope for a variable named varname. That is, the class Test does not generate a local variable classa_dict, but it uses the global variably classa_dict instead.

    So, global is nothing to give at declaration to tell python that the variable can be used everywhere. It much more tells Python that there is already some variable with this name, that is referred to.

    This means, in testmodule.ty the line global classa_dict means: Look somewhere in the module to find a variable classa_dict and operate on this variabla instead of creating a new local variable classa_dict.

    In test.py, then you can assign classa_dict = testmodule.classa_dict which tells python that lclassa_dict refers to the classa_dict in testmodule.py. Therefore, you don’t need a global in test.py, because, you simply modify testmodule.classa_dict when you assign something to lclassa_dict. Then, the class Test in testmodule.py knows that it should look to testmodule.classa_dict because it uses the global.

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

Sidebar

Related Questions

I need to get the path to the native (rather than the WOW) program
EDIT: WOW. This question is 12 years old now. As someone stated, it can
UPDATE 1: I haven't figured out what's going on, but this definitely seems to
Wow...I've never seen this before. Any way to get around it? foreach( double r
Wow, it's hard to find a simple explanation to this topic. A simple many-to-many
WOW! That's a weird title, but I'm happy you're looking, cause I couldn't find
Wow crazy title! But here's the problem. In efforts to dry up my school
My work just gave me a quad core computer, and WOW build times are
I have been stuck on this all weekend and failed miserably! Please help me
Wow, what a stupid question you might say. But is it possible? I have

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.