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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:59:18+00:00 2026-05-26T05:59:18+00:00

I have a large python code with many modules and classes. I have a

  • 0

I have a large python code with many modules and classes. I have a special class, whose single instance is needed everywhere throughout the code (it’s a threaded application, and that instance of a class also holds Thread Local Storage, locks, etc). It’s a bit uncomfortable to always “populate” that instance in every imported module. I know, using globals is not the best practice, but anyway: is there any “import hook” in python, so I can do with hooking on it to have my instance available in every modules without extra work? It should work for normal imports, “from mod import …” too, and for import constructs too. If this is not possible, can you suggest a better solution? Certenly it’s not fun to pass that instance to the constructors of every classes, etc … Inheritance also does not help, since I have modules without classes, and also I need a single instance, not the class itself …

class master():
    def import_module(self, name):
        mod = __import__(name)
        mod.m = self
        return mod
[...]
m = master()

Currently I am thinking something like that: but then I have to use m.import_module() to import modules, then other modules will have instance of master class with name of “m” available, so I can use m.import_module() too, etc. But then I have to give up to use “normal” import statements, and I should write this:

example_mod = m.module_import("example_mod")

instead of just this:

import example_mod

(but for sure I can do with this too, to assign “m” to example_mod.m then)

  • 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-26T05:59:19+00:00Added an answer on May 26, 2026 at 5:59 am

    Certainly it’s not fun to pass that instance to the constructors of
    every classes

    You don’t have to do this. Set up your global class in a module like config and import it

    # /myapp/enviroment/__init__.py
    
    class ThatSingleInstanceClass: pass
    
    # create the singleton object directly or have a function init the module
    singleton = ThatSingleInstanceClass() 
    
    
    
    # /myapp/somewhere.py
    
    # all you need to use the object is importing it
    from myapp.enviroment import singleton
    
    class SomeClass:
    
        def __init__(self): # no need to pass that object
            print "Always the same object:", singleton
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of a large python program I have the following code: for arg
I have a large amount of python code that tries to handle numbers with
I have a python code which reads many files. but some files are extremely
The hindrance we have to ship python is the large size of the standard
I have a Python program that processes fairly large NumPy arrays (in the hundreds
I have this Python code to do this: from struct import pack as _pack
I have a large infrastructure that is written in Python 2.6, and I recently
So I'm developing a rather large python project with a number of modules. The
I have many large (>35,000,000) lists of integers that will contain duplicates. I need
I have started doing a code review for someone, and there is a large

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.