Created
March 16, 2017 09:55
-
-
Save judy2k/f71c4cde26ed3a06f93e19d17af9c093 to your computer and use it in GitHub Desktop.
Monkey Patching Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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