usr bin python test py import modfoo def replacement self print Alien

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
# test.py
import modfoo
def replacement(self):
print 'Alien method'
def patching():
import modbar
modbar.Bar.f = replacement
obj = modfoo.Foo()
obj.problem()
if __name__ == '__main__':
patching()
# modfoo.py =======================
class Foo:
def problem(self):
from modbar import Bar
b = Bar()
b.f()
# modbar.py =======================
class Bar:
def f(self):
print 'Bar.f'