是否可以在不修改给定模块的情况下覆盖内部模块功能?你知道吗
下面的例子是我能想象到的最好的简化。对于原来的问题,请看问题的结尾。你知道吗
我有一个具有以下结构的包:
a
├── b.py
├── c.py
└── __init__.py
其中b.py是
from c import c_func
def b_func():
print('b.b_func')
return c_func()
而c.py是
def c_func():
print('c.c_func')
return 'return_c'
我想修改外部主.py
import a
from a.b import b_func
print('Calling b_func without modification')
solution = b_func()
print(solution)
# Trying to modify the internal function
print('Calling b_func with modification')
old_c_func = a.c.c_func
def new_c_func(*args, **kwargs):
print('do something in new_c_func')
return('return_new_c')
a.c.c_func = new_c_func
solution = b_func()
print(solution)
前面的代码输出以下内容
Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
c.c_func
return_c
但我希望
Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
do something in new_c_func
return_new_c_func
最初的问题与Scipy的一个私有函数有关,但我假设我的问题的答案概括为以下问题:
import scipy
# Stack calls
# minimize calls scipy.optimize._minimize_trust_ncg
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_minimize.py#L463
# _minimize_trust_ncg calls scipy.optimize._trustregion._minimize_trust_region
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_trustregion_ncg.py#L39
from scipy.optimize import minimize
old_minimize_trust_region = scipy.optimize._trustregion._minimize_trust_region
def new_minimize_trust_region(*args, **kwargs):
print('new function')
return old_minimize_trust_region
scipy.optimize._trustregion._minimize_trust_region = new_minimize_trust_region
x0 = [2]
fun = lambda x: x**2 + 42
jac = lambda x: 2*x
hess = lambda x: 2
method = 'trust-ncg'
solution = minimize(fun, x0, method=method, jac=jac, hess=hess)
print(solution)
我通过更改要修改的函数的属性code找到了临时解决方案。以下代码按需要工作:
我仍然缺少一个完整的解释,也不知道代码是否合适,但它是按预期工作。我被Stackoverflow问题Can you patch just a nested function with closure, or must the whole outer function be repeated?启发了
相关问题 更多 >
编程相关推荐