如何在C中提取IronPython脚本参数的名称#

2024-06-16 10:06:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python脚本,如下所示:

def VSH(GR, GRsand, GRshale): 
    '''Calculates Vsh from gamma inputs'''
    value = (((GR-GRsand)/(GRshale-GRsand)))
    if (value > 100.0):
      value = 100.0
    elif (value < 0.0):
        value = 0.0
    return value

在C中,我有一个循环,在ironpython2.2中,它将提取3个参数。在

^{pr2}$

现在在IronPython2.7.5中,我得到了4个变量的名称,这是有意义的,但是破坏了旧代码。从手表上我得到:

co_varnames tuple, 4 items  IronPython.Runtime.PythonTuple
[0] "GR"    object {string}
[1] "GRsand"    object {string}
[2] "GRshale"   object {string}
[3] "value" object {string}

看着物体inputFunction.func_代码在调试器中,我没有看到任何只返回参数的内容。我确实看到属性co_argcount=3。如果我可以确定参数总是在变量列表中的第一个,那么我就可以用它过滤掉局部变量。有什么建议吗?在


Tags: 代码from脚本参数stringobjectvaluedef
1条回答
网友
1楼 · 发布于 2024-06-16 10:06:29

我的解决方案是:

// using System.Reflection;
dynamic func = scope.GetVariable("VSH");
var code = func.__code__;
var argNamesProperty = code.GetType().GetProperty("ArgNames", BindingFlags.NonPublic | BindingFlags.Instance);
string[] argNames = (string[])argNamesProperty.GetValue(code, null);
// argNames = ["GR", "GRsand", "GRshale"]

您查找的位置正确,但不幸的是,IronPython.Runtime.FunctionCode.ArgNames属性是私有的。有了反射,我们可以忽略它,只需获取参数名。在

以下是我的完整测试设置:

^{pr2}$

我确信您可以删减dynamic func = ...行之前的所有内容,因为您可能已经有权访问您的VSH函数。在

相关问题 更多 >