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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:36:12+00:00 2026-06-05T10:36:12+00:00

I create a dictionary from a remote database as part of my application run.

  • 0

I create a dictionary from a remote database as part of my application run. This process is pretty I/O heavy, so I’ve decided to create a “singleton” instance of this dictionary and just call it as it is needed in my application.

The code looks like (in Dictionaries.py):

state_code_dict = None

def get_state_code_dict():
    global state_code_dict
    if state_code_dict == None:
        state_code_dict = generate_state_code_dict()
    return state_code_dict

I then import and call the get_state_code_dict() function where needed. I added a print statement to check if state_code_dict was being reinitialized or reused, and I found it was being reused (which is the functionality I want). Why is the instance of state_code_dict surviving the application run?

Edit

I import the get_state_code_dict function in multiple files.

  • 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-05T10:36:14+00:00Added an answer on June 5, 2026 at 10:36 am

    This is the Python Language Reference’s description of how importing a module works:

    (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace

    (Emphasis added.) Here, initializing a module means executing its code. This execution is only performed if necessary, i.e. if the module was not previously imported in the current process. Since Python modules are first-class runtime objects, they effectively become singletons, initialized at the time of first import.

    Note that this means that there’s no need for a get_state_dict_code function; just initialize state_code_dict at top-level:

    state_code_dict = generate_state_code_dict()
    

    For a more in-depth explanation, see this talk by Thomas Wouters, esp. the first part — around 04:20 — where he discusses the “everything is runtime” principle.

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

Sidebar

Related Questions

I am trying to create a Python dictionary from a stored list. This first
I am trying to create a dictionary from 2 lists where one list contains
Alright well I am trying to create a dictionary from a text file so
Is it possible to create an object from a dictionary in python in such
I have C# Dictionary and I want create a .csv file from it. For
I'm attempting to create a dictionary of hours based off of this calendar: http://disneyworld.disney.go.com/parks/magic-kingdom/calendar/
I'd like to create a dictionary from a text file that I have, who's
I am trying to create a dictionary from a csv file. The first column
This program is meant to read in a csv file and create a dictionary
What is the easiest way to create a dictionary from an iterable and assigning

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.