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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:02:21+00:00 2026-06-15T06:02:21+00:00

I am reorganizing my code and therefore creating new namespaces. I’m changing static classes

  • 0

I am reorganizing my code and therefore creating new namespaces. I’m changing “static” classes (classes with @staticmethod in each method) for modules. This is the way to go, right?

The problem is that I have doubts on how to share the resources between these modules.

Let’s say I had a module from which I was doing all connections to database, and of course all classes/methods were sharing the variable which stored the DB cursor (I’m using SQLite). Now, in different modules, they also have to share the cursor.

Graphical representation of dependences

So, my ideas:

  • Declare the global variable in each module. But globals are evil, eat children and steal our jobs. So I don’t know if this is the way to go.

    '''Sub Module 1'''
    
    global database_cursor
    
  • Import the “father” database_module with the original database_cursor and use something like this:

    '''Sub Module 1'''
    
    db_cursor = database_module.database_cursor
    

This second looks fine in this case, but I think in many cases will lead to recursive imports, which I guess it´s something to avoid.

  • 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-15T06:02:22+00:00Added an answer on June 15, 2026 at 6:02 am

    Your second method is the way to go. Python imports are singleton by nature. When a module is imported multiple times it is only executed the first time. Subsequent imports fetch the module object instance from the globals. More on that here.

    shared.py:

    class Shared:
        def __init__(self):
            print("Init shared")
    
        def do_stuff(self, from_mod):
            print("Do stuff from {0}. I am instance {1}".format(from_mod, self))
    
    shared = Shared()
    

    foo.py

    import shared
    
    shared.shared.do_stuff("foo")
    

    bar.py

    import foo
    import shared
    
    shared.shared.do_stuff("bar")
    

    If we execute bar.py we get:

    >>> Init shared
    >>> Do stuff from foo. I am instance <shared.Shared instance at 0x10046df38>
    >>> Do stuff from bar. I am instance <shared.Shared instance at 0x10046df38>
    

    So in your case you can reference database_module from anywhere you want and it gets initialized only once, therefore effectively sharing your connection.

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

Sidebar

Related Questions

We are currently reorganizing our source code, moving stuff around in a new directory
I'm reorganizing our MySQL database, changing MyISAM tables to InnoDB and setting foreign keys,
This question is about what happens with the reorganizing of data in a clustered
I tend to use Regionerate quite extensively for reorganizing my code files. My only
I just started a new project and was reorganizing the source structure including renaming
I have been reorganizing some code and would like to use a program to
I am reorganizing all my js introducing namespaces with a main script (using jQuery)
I am in need of reorganizing a large CSV file. The first column, which
I'm in the process of reorganizing some of the legacy libraries in our application
I have found myself countless times reorganizing our solution (72 + projects), in efforts

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.