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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:05:59+00:00 2026-05-14T19:05:59+00:00

What are the best practices for extending an existing Python module – in this

  • 0

What are the best practices for extending an existing Python module – in this case, I want to extend the python-twitter package by adding new methods to the base API class.

I’ve looked at tweepy, and I like that as well; I just find python-twitter easier to understand and extend with the functionality I want.

I have the methods written already – I’m trying to figure out the most Pythonic and least disruptive way to add them into the python-twitter package module, without changing this modules’ core.

  • 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-14T19:06:00+00:00Added an answer on May 14, 2026 at 7:06 pm

    A few ways.

    The easy way:

    Don’t extend the module, extend the classes.

    exttwitter.py

    import twitter
    
    class Api(twitter.Api):
        pass 
        # override/add any functions here.
    

    Downside : Every class in twitter must be in exttwitter.py, even if it’s just a stub (as above)

    A harder (possibly un-pythonic) way:

    Import * from python-twitter into a module that you then extend.

    For instance :

    basemodule.py

     class Ball():
        def __init__(self,a):
            self.a=a
        def __repr__(self):
            return "Ball(%s)" % self.a
    
    def makeBall(a):
        return Ball(a)
    
    def override():
        print "OVERRIDE ONE"
    
    def dontoverride():
        print "THIS WILL BE PRESERVED"
    

    extmodule.py

    from basemodule import *
    import basemodule
    
    def makeBalls(a,b):
        foo = makeBall(a)
        bar = makeBall(b)
        print foo,bar
    
    def override():
        print "OVERRIDE TWO"
    
    def dontoverride():
        basemodule.dontoverride()
        print "THIS WAS PRESERVED"
    

    runscript.py

    import extmodule
    
    #code is in extended module
    print extmodule.makeBalls(1,2)
    #returns Ball(1) Ball(2)
    
    #code is in base module
    print extmodule.makeBall(1)
    #returns Ball(1)
    
    #function from extended module overwrites base module
    extmodule.override()
    #returns OVERRIDE TWO
    
    #function from extended module calls base module first
    extmodule.dontoverride()
    #returns THIS WILL BE PRESERVED\nTHIS WAS PRESERVED
    

    I’m not sure if the double import in extmodule.py is pythonic – you could remove it, but then you don’t handle the usecase of wanting to extend a function that was in the namespace of basemodule.

    As far as extended classes, just create a new API(basemodule.API) class to extend the Twitter API module.

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

Sidebar

Related Questions

What are the best practices for creating exceptions? I just saw this, and I
What are the best practices of changing widget layout in new (updated) version of
With best practices in mind, what's the advisable approach to adding optional parameters to
What are best practices to accomplish this task: I have commits: A -- B
So I have a question of best practice in terms of extending functionality of
Best practices or tools for installing a SQL Server database I have a SQL
About best practices on designing stored procedures, should a stored procedure updating a record
What best practices have you used in unit testing embedded software that are peculiar
I am looking for best practices of avoiding constructor injection overuse. For example I
Its more of a best practices sort of question. Here it goes... I have

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.