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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:36:50+00:00 2026-06-09T19:36:50+00:00

In Python, the term monkey patch only refers to dynamic modifications of a class

  • 0

In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, As a beginner its really difficult to me understand this term in context of python. Can anybody explain to me with a real world example How exactly we do ?

  1. dynamic modifications of a class
  2. dynamic modifications of module at runtime

I am insisting a real world example (as simple as possible) to understand in which scenarios we have to do such task ?

  • 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-09T19:36:51+00:00Added an answer on June 9, 2026 at 7:36 pm

    Monkey-patching is a way to make some global under-the-hood change in a way that existing code will continue to run, but with modified behavior.

    A really really trivial example of changing the behavior of the builtin str command:

    b.py

    def foo(msg):
        s = str(msg)
        print s, type(s)
    

    a.py

    import b
    
    b.foo('foo')
    
    # monkey-patch 
    import __builtin__
    __builtin__.str = unicode
    
    b.foo('foo')
    
    # Results:
    #foo <type 'str'>
    #foo <type 'unicode'>
    

    The a module has modified the behavior of other code using the str command, by patching it to use unicode instead. This would be necessary since we pretend that we have no access to b.py‘s code. It could have been a huge package that we simply use and can’t change. But we can slip in new code to be called that changes the behavior.

    A real world example from gevent

    >>> import gevent
    >>> from gevent import socket
    >>> urls = ['www.google.com', 'www.example.com', 'www.python.org']
    >>> jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]
    >>> gevent.joinall(jobs, timeout=2)
    >>> [job.value for job in jobs]
    ['74.125.79.106', '208.77.188.166', '82.94.164.162']  
    

    The example above used gevent.socket for socket operations. If the
    standard socket module was used it would took it 3 times longer to
    complete because the DNS requests would be sequential. Using the
    standard socket module inside greenlets makes gevent rather pointless,
    so what about module and packages that are built on top of socket?

    That’s what monkey patching for. The functions in gevent.monkey
    carefully replace functions and classes in the standard socket module
    with their cooperative counterparts. That way even the modules that
    are unaware of gevent can benefit from running in multi-greenlet
    environment.

    >>> from gevent import monkey; monkey.patch_socket()
    >>> import urllib2 # it's usable from multiple greenlets now
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm familiar with Python generators, however I've just come across the term generative method
Python 3 really complicated the whole file reading process, when you have a binary
Python beginner here, I have a list of lists and want to refer to
Python beginner here. I have a text file that is sorted into columns: fields
After finding the difflib.SequenceMatcher class in Python's standard library to be unsuitable for my
First, I am aware that dynamic languages is a term used mainly by a
What Python version can you please recommend for a long-term (years) project? Should one
I have a few long term processes and temporary processes in Python. While shell
Where can I find the mid-term or long-term road map for python. By which,
Perl has the excellent module Term::ANSIScreen for doing all sorts of fancy cursor movement

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.