【发布时间】:2019-08-23 21:24:55
【问题描述】:
所以我想把下面的 Python 代码翻译成 VBA(只是玩具例子来说明问题,细节省略)。
def numerical_integration(fun, a, b):
"""
:param fun: callable
:param a, b: float
:return: float
"""
# do something based on both fun and a, b
res = ...
return res
def h(x, k):
"""
helper func
:param x: float, main arg
:param k: float, side param
:return: float
"""
# calc based on both x and k
res = ...
return res
def main_function(k):
"""
:param k: float
:return: float
"""
a, b = 0.0, 1.0
res = numerical_integration(fun=lambda x: h(x, k), a=a, b=b)
return res
问题是我不知道如何正确地将“部分”函数之类的东西作为 VBA 中的参数传递。我在this post 中找到了如何在 VBA 中传递“整个”函数。这个想法是将函数作为字符串传递,然后让 VBA 评估字符串(我猜类似于 Python eval 等价物?)。但是我不知道如果我的函数是部分的,这将有什么帮助,即我希望它只作为第一个参数的函数,例如 lambda x: f(x, k),其中第二个参数在主函数的上下文中。
提前感谢您的任何建议。
【问题讨论】:
-
@ChristopherPeisert 我就是从那里来的。但是,它并不能解决“部分”功能问题。我不能简单地使用字符串调用 whole 函数。