Skip to content

Instantly share code, notes, and snippets.

@judy2k
Created March 16, 2017 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judy2k/f71c4cde26ed3a06f93e19d17af9c093 to your computer and use it in GitHub Desktop.
Save judy2k/f71c4cde26ed3a06f93e19d17af9c093 to your computer and use it in GitHub Desktop.
Monkey Patching Functions
# So, a bit like mocking/patching, it depends on how the thing you're patching is imported *where it's used*.
# If the code that uses it is in a module called `client`, you'd want to do one of the following:
# If client.py looks like this:
from django.things import victim
def useful_function():
victim(blah)
# ... then you'd monkey patch it like this in your own code:
import client # ensure that client is loaded into memory
client.victim = my_monkey # Replace the top-level variable with your own value
# But if client.py looks like this:
import django.things
def useful_function():
django.things.victim(blah)
# ... then you need to patch it in `things`, so you do this:
import django.things # ensure django.things is loaded
django.things.victim = my_monkey # Replace its top-level variable with your own value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment