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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:08:23+00:00 2026-06-04T09:08:23+00:00

I am trying to give a slight amount of genericness to my code .

  • 0

I am trying to give a slight amount of genericness to my code . Basically what I am looking for is this .

I wish to write an API interface MyAPI :

class MyAPI(object):
    def __init__(self):
       pass

    def upload(self):
       pass

    def download(self):
      pass

class MyAPIEx(object):
   def upload(self):
      #specific implementation

class MyAPIEx2(object): 
   def upload(self)
    #specific implementation

#Actual usage ... 
def use_api():
     obj = MyAPI()
     obj.upload()

SO what I want is that based on a configuration I should be able to call the upload function
of either MyAPIEx or MyAPIEx2 . What is the exact design pattern I am looking for and how do I implement it in python.

  • 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-04T09:08:25+00:00Added an answer on June 4, 2026 at 9:08 am

    Its really hard to say what pattern you are using, without more info. The way to instantiate MyAPI is indeed a Factory like @Darhazer mentioned, but it sounds more like you’re interested in knowing about the pattern used for the MyAPI class hierarchy, and without more info we cant say.

    I made some code improvements below, look for the comments with the word IMPROVEMENT.

    class MyAPI(object):
        def __init__(self):
           pass
    
        def upload(self):
           # IMPROVEMENT making this function abstract
           # This is how I do it, but you can find other ways searching on google
           raise NotImplementedError, "upload function not implemented"
    
        def download(self):
           # IMPROVEMENT making this function abstract
           # This is how I do it, but you can find other ways searching on google
           raise NotImplementedError, "download function not implemented"
    
    # IMPROVEMENT Notice that I changed object to MyAPI to inherit from it
    class MyAPIEx(MyAPI):
       def upload(self):
          #specific implementation
    
    # IMPROVEMENT Notice that I changed object to MyAPI to inherit from it
    class MyAPIEx2(MyAPI): 
       def upload(self)
          #specific implementation
    
    
    # IMPROVEMENT changed use_api() to get_api(), which is a factory,
    # call it to get the MyAPI implementation
    def get_api(configDict):
         if 'MyAPIEx' in configDict:
             return MyAPIEx()
         elif 'MyAPIEx2' in configDict:
             return MyAPIEx2()
         else
             # some sort of an error
    
    # Actual usage ... 
    # IMPROVEMENT, create a config dictionary to be used in the factory
    configDict = dict()
    # fill in the config accordingly
    obj = get_api(configDict)
    obj.upload()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Am trying to give an uploaded image a nicer path, using this code
I am trying to give the effect of general headings in this table and
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
I am trying to give an alternate class to each LI foreach. i.e. <li
I'm trying give a Welcome Message to my users with that: #welcome_controller.rb class WelcomeController
I'm basically trying to give rubberbanding effect to my drawing, I'm using the setCompositionMode
Ive been trying to give style to button, The code that I ve used
I'm trying to give a bold font to an input this way below but
Basically I am trying to give a user a certain password so I can
While building a RESTful API of my web service I'm trying to give clients

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.