1

So I have a package in my virtual environment installed in the site-packages folder of it. In that package there is a module with a bunch of functions, I want to change one of these functions without touching the package code, so I want to make a hook of that module and then change/overwrite the function so it behaves in the way I want.

I don't know if this is even posible in python (I'm new to the language)

Example:

I have a package called their_package in my site-packages folder, inside I have a module named whatever.py with the following code:

import sys
import os

def this_is_a_function(parameter):
    //whatever logic in here
    return result

What I want is to hook the whatever.py module so I can redefine the this_is_a_function to have a diferent logic. Is this posible? I would appreciate a lot a code sample!

Thank you in advance!:)

Daniel Ramos
  • 137
  • 1
  • 8
  • How do you plan to use that hooked module later? Do you want to hook it only for one scenario (piece of logic), or for the whole Python interpreter session? – Roman Bodnarchuk Apr 08 '14 at 09:55

1 Answers1

3

You can redefine your function with:

import whatever

def this_is_a_function(parameter):
    pass

whatever.this_is_a_function = this_is_a_function
fredtantini
  • 15,966
  • 8
  • 49
  • 55