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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:00:17+00:00 2026-05-16T10:00:17+00:00

Over time I found the need to override several stdlib methods from Python in

  • 0

Over time I found the need to override several stdlib methods from Python in order to overcome limitation or to add some missing functionality.

In all cases I added a wrapper function and replaced the original method from the module with my wrapper (the wrapper was calling the original method).

Why I did this? Just to be sure that all the calls to the method are using my new versions, even if these are called from other third-party modules.

I know that monkeypatching can be a bad thing but my question is if this is useful if you use it with care? Meaning that:

  • you still call the original methods, assuring that you are not missing anything when the original module is updated
  • you are not changing the original “meaning” of the methods

Examples:

  • add coloring support to python logging module.
  • make open() be able to recognize Unicode BOM masks when using text mode
  • adding logging support to os.system() or subprocess.Popen() – letting you output to console or/and redirect to another file.
  • implementing methods that are missing on your platform like os.chown() or os.lchown() that are missing on Windows.

Doing things like these appear to me as decent overrides but I would like to see how others are seeing them and specially what should be considered as an acceptable monkeypatch and what not.

  • 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-16T10:00:17+00:00Added an answer on May 16, 2026 at 10:00 am

    Monkeypatching can be “the least of evils”, sometimes — mostly, when you need to test code which uses a subsystem that is not well designed for testability (doesn’t support dependency injection &c). In those cases you will be monkeypatching (very temporarily, fortunately) in your test harness, and almost invariably monkeypatching with mocks or fakes for the purpose of isolating tests (i.e., making them unit tests, rather than integration tests).

    This “bad but could be worse” use case does not appear to apply to your examples — they can all be better architected by editing the application level code to call your appropriate wrapper functions (say myos.chown rather than the bare os.chown, for example) and putting your wrapper functions in your own intermediate modules (such as myown) that stand between the application level code and the standard library (or third-party extensions that you are thus wrapping — there’s nothing special about the standard library in this respect).

    One problematic situation might arise when the “application level code” isn’t really under your control — it’s a third party subsystem that you’d rather not modify. Nevertheless, I have found that in such situations modifying the third party subsystem to call wrappers (rather than the standard library functions directly) is way more productive in the long run — then of course you submit the change to the maintainers of the third party subsystem in question, they roll your change into their subsystem’s next release, and life gets better for everybody (you included, since once your changes are accepted they’ll get routinely maintained and tested by others!-).

    (As a side note, such wrappers may also be worth submitting as diffs to the standard library, but that is a different case since the standard library evolves very very slowly and cautiously, and in particular on the Python 2 line will never evolve any longer, since 2.7 is the last of that line and it’s feature-frozen).

    Of course, all of this presupposes an open-source culture. If for some mysterious reasons you’re using a closed-source third party subsystem, therefore one which you cannot possibly maintain, then you are in another situation where monkey patching may be the lesser evil (but that’s just because the evil of losing strategic control of your development by trusting in code you can’t possibly maintain is such a bigger evil in itself;-). I’ve never found myself in this situation with a third-party package that was both closed-source and itself written in Python (if the latter condition doesn’t hold your monkeypatches would do you no good;-).

    Note that here the working definition of “closed-source” is really very strict: for example, even Microsoft 12+ years ago distributed sources of libraries such as MFC with Visual C++ (as their product was then called) — closed-source because you couldn’t redistribute their sources, but still, you DID have sources at hand, so when you met some terrible limitation or bug you COULD fix it (and submit the change to them for a future release, as well as publishing your change as a diff as long as it included absolutely none of their copyrighted code — not trivial, but feasible).

    Monkeypatching well beyond the strict confines within which such an approach is “the least of evil” is a frequent mistake of users of dynamic languages — be careful not to fall into that trap yourself!

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok I figured out what needed to be done to… May 16, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer I think that you want SCMBug and possibly also the… May 16, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer The solution in the other thread should work but you… May 16, 2026 at 11:27 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 working on a project were we need more performance. Over time we've continued
My memory usage increases over time and restarting Django is not kind to users.
I have made this mistake several times, and found myself referencing This Post every
I need to improve memory performance on my application and I could see that
I have the following challenge, and I haven't found a good answer. I am
I've run into reoccuring problem for which I haven't found any good examples or
i'm stuck with a seemingly simple problem in mathematics: i need to rotate points
Possible Duplicate: c++ using too much cpu my game uses over 50% of cpu.
I suspect this could be something faulty with Excel (although I keep an open
I find myself constantly running into a situation where I have a set of

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.