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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:07:19+00:00 2026-06-13T15:07:19+00:00

I have a class that can interface with either Oracle or MySQL. The class

  • 0

I have a class that can interface with either Oracle or MySQL. The class is initialized with a keyword of either “Oracle” or “MySQL” and a few other parameters that are standard for both database types (what to print, whether or not to stop on an exception, etc.).

It was easy enough to add if Oracle do A, elif MySQL do B as necessary when I began, but as I add more specialized code that only applies to one database type, this is becoming ugly. I’ve split the class into two, one for Oracle and one for MySQL, with some shared functions to avoid duplicate code.

What is the most Pythonic way to handle calling these new classes? Do I create a wrapper function/class that uses this same keyword and returns the correct class? Do I change all of my code that calls the old generic class to call the correct DB-specific class?

I’ll gladly mock up some example code if needed, but I didn’t think it was necessary. Thanks in advance for any help!

  • 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-13T15:07:21+00:00Added an answer on June 13, 2026 at 3:07 pm

    Do I create a wrapper function/class that uses this same keyword and returns the correct class?

    That’s the idea. Such a function is called a factory function:

    def connect_to(db, *args):
        if db == "MySQL":
            return MySQL(*args)
        elif db == "Oracle":
            return Oracle(*args)
        else:
            raise ValueError("unknown database type: %r" % db)
    

    Make sure that both database classes have the same API. You can achieve this either with duck typing or with abstract base classes (ABCs); the latter are mostly useful if functionality is shared between the classes, or if you want to do isinstance checks to find out if an object represents a database connection.

    In the case of shared functionality, the template method pattern often comes in handy.

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

Sidebar

Related Questions

I have a Base class that needs a Generic type. That can be either
Let me explain best with an example. Say you have node class that can
I have a class that I can have many instances of. Inside it creates
Here's the scenario: I'd like to have a host class that can have a
I have a class in C++ that I can't modify. However, that class holds
I have a class which contains a static collection that can be used across
In a client application I have a DBIx::Class model 'Todo' that can be linked
I have a simple class with an attribute that can contain a list of
I have a class Something that implements ISomething . How can I convert/cast from
I have a Version class that contains a version_number string, and can obtain a

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.