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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:58:39+00:00 2026-05-16T17:58:39+00:00

If I have a seperate class for my db calls, and I create another

  • 0

If I have a seperate class for my db calls, and I create another implementation of the db layer but say with a different data store.

Is there a way for me to completly swap out the implementation without having to change allot of code?

i.e. I am starting a project, so I can design things properly to achieve this from the get-go.

Note: I will use this pattern for other parts of the site also, not just the db layer so its not really specific to db layer only.

  • 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-16T17:58:40+00:00Added an answer on May 16, 2026 at 5:58 pm

    As long as two modules implement exactly the same interface (classes with the same names, methods, and other attributes, functions with the same names and signatures, …) you can pick one or the other at the time your application is starting up, for example on the basis of some configuration file, and import the chosen one under a fixed name. All the rest of your application can then use that fixed name and, net of the startup code, be blissfully unaware of any shenanigans that may have been done at the start.

    For example, consider a simplified case:

    # english.py
    def greet(): return 'Hello!'
    
    # italian.py
    def greet(): return 'Ciao!'
    
    # french.py
    def greet(): return 'Salut!'
    
    # config.py
    langname = 'italian'
    
    # startit.py
    import config
    import sys
    lang = __import__(config.langname)
    sys.modules['lang'] = lang
    

    Now, all the rest of the application can just import lang, and it will be getting under that name the italian module, so, when calling lang.greet(), it will get the string 'Ciao!'.

    Of course, in real life you’ll have multiple modules, each with multiple functions, classes, and whatnot, but the general principles stay very similar. Just take special care about modules with qualified names (such as foo.bar), i.e., modules which must reside in a package (in this case, foo). For those, you can’t just use __import__‘s return value, but must use a slightly more roundabout approach, such as:

    import sys
    def importanyasname(actualname, fakename):
        __import__(actualname)
        sys.modules[fakename] = sys.modules[actualname]
    

    that is, ignore __import__‘s return value, and reach right for the value that’s left (with the actual name as the key) in the sys.modules dictionary — that is the module object you seek, and that you can set back into sys.modules with the “fake name” by which all the rest of the application will be able to blissfully import it any time.

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

Sidebar

Related Questions

I have a class that has two method in it, one calls a class
Suppose you have two seperate ASP.NET Web Application projects that both need to use
I have a method running in a seperate thread. The thread is created and
i have a log file which contains hundreds/thousands of seperate XML messages and need
I am trying to have a table header, in a seperate div stay in
I have monthly sales figures stored in separate sheets. I would like to create
I have a plugin that creates an object that has an event. Is there
I have 2 separate java services that use a complex type that is a
I have basically two separate sites, a SharePoint collaboration site, and an ASP.Net application
I have several somewhat separate programs, that conceptually can fit into a single project.

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.