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 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

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Scala programs require at a minimum the scala-library.jar file that… May 16, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer You can use the [NSMenu - setView:] method for it.… May 16, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Assuming you have MonoDevelop and MonoTouch installed, you can build… May 16, 2026 at 5:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm fairly new to Perl and was wondering what the best practices regarding subroutines
I'm looking for a set of best practices to use when implementing IModelBinder .
Anyone have any best practices tips for Rails and sessions? The default session type
What are the best practices for working with Sql server Xml columns to ensure
From a practical and best practices perspective, should rails models that have HABTM association
Possible Duplicate: Best practices for validating email address in Objective-C on iOS 2.0? I
Anybody can provide me the best practices to be followed in deployment of asp.net
My question(s) is one of best practices for OOP. Im using Codeigniter framework/PHP. I
I'm really trying to use best-practices for dependencies management in my web apps but
There are lots of GUI best practices. I am looking for best practices when

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.