[Solved]-How do I redefine functions in python?

13๐Ÿ‘

โœ…

import module1
import unittest

class MyTest(unittest.TestCase):
    def setUp(self):
        # Replace othermod.function with our own mock
        self.old_func1 = module1.func1
        module1.func1 = self.my_new_func1

    def tearDown(self):
        module1.func1 = self.old_func1

    def my_new_func1(self, x):
        """A mock othermod.function just for our tests."""
        return True

    def test_func1(self):
        module1.func1("arg1")

Lots of mocking libraries provide tools for doing this sort of mocking, you should investigate them as you will likely get a good deal of help from them.

๐Ÿ‘คNed Batchelder

5๐Ÿ‘

import foo

def bar(x):
    pass

foo.bar = bar
๐Ÿ‘คleoluk

2๐Ÿ‘

Just assign a new function or lambda to the old name:

>>> def f(x):
...     return x+1
... 
>>> f(3)
4
>>> def new_f(x):
...     return x-1
... 
>>> f = new_f
>>> f(3)
2

It works also when a function is from another module:

### In other.py:
# def f(x):
#    return x+1
###

import other

other.f = lambda x: x-1

print other.f(1)   # prints 0, not 2
๐Ÿ‘คsastanin

2๐Ÿ‘

Use redef: http://github.com/joeheyming/redef

import module1
from redef import redef

rd_f1 = redef(module1, 'func1', lambda x: True)

When rd_f1 goes out of scope or is deleted, func1 will go back to being back to normal

๐Ÿ‘คJoe Heyming

1๐Ÿ‘

If you want to reload into the interpreter file foo.py that you are editing, you can make a simple-to-type function and use execfile(), but I just learned that it doesnโ€™t work without the global list of all functions (sadly), unless someone has a better idea:

Somewhere in file foo.py:

def refoo ():
  global fooFun1, fooFun2
  execfile("foo.py")

In the python interpreter:

refoo() # You now have your latest edits from foo.py

Leave a comment