Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python内部更改函数输入名称_Python - Fatal编程技术网

Python内部更改函数输入名称

Python内部更改函数输入名称,python,Python,我需要创建一个函数,该函数可以: 检查输入参数并获取输入数量及其值 创建一个循环,并在每个输入名称后添加字符串“\u in” 使新的投入全球化 用旧值分配新输入 我尝试使用inspect.getargspec(),但似乎应该在函数外部调用它。有什么建议吗 以下是伪代码: 定义此功能: def set_global(a1, a2, a3, .... an): #the number of arguments is not fixed and we need to detect that

我需要创建一个函数,该函数可以:

  • 检查输入参数并获取输入数量及其值
  • 创建一个循环,并在每个输入名称后添加字符串“\u in”
  • 使新的投入全球化
  • 用旧值分配新输入
  • 我尝试使用inspect.getargspec(),但似乎应该在函数外部调用它。有什么建议吗

    以下是伪代码: 定义此功能:

    def set_global(a1, a2, a3, .... an):
        #the number of arguments is not fixed and we need to detect that
        for old_input in total input:
             new_input_name=str(old_input.name)+'_in'
             global new_input_name 
             new_input_name = old_input.value
    
    def do_something_with_globals(**kwargs):
        for k,v in kwargs.items():
            globals()[k+'_in'] = v
    
    我们可以将此应用于
    globals()
    ,以执行您想要的操作:

    def set_globals(**kwargs):
        for argname in kwargs:
            globals()['%s_in' % argname] = kwargs[argname]
    
    因此,使用Python交互式解释器中的上述函数:

    >>> set_globals(foo='x', bar='y')
    >>> foo_in
    'x'
    >>> bar_in
    'y'
    

    我真的认为您正在寻找
    globals
    函数

    a = 1
    globals()['a'] = 2
    print a  #2
    globals()['a_in'] = 2
    print a_in  #2
    
    您可以将其放在函数中:

    def set_global(a1, a2, a3, .... an):
        #the number of arguments is not fixed and we need to detect that
        for old_input in total input:
             new_input_name=str(old_input.name)+'_in'
             global new_input_name 
             new_input_name = old_input.value
    
    def do_something_with_globals(**kwargs):
        for k,v in kwargs.items():
            globals()[k+'_in'] = v
    
    可以这样称呼:

    a = 1
    b = 2
    do_something_with_globals(a=a,b=b)
    print a_in
    print b_in
    

    但是,老实说,我真的不认为这是一个好主意…

    您的用例是什么?您可能想在这里使用装饰器。@ThomasOrozco:我用一个坏函数更新了我的帖子。有没有一种明智的方法可以用任何语言动态创建局部变量?@mgilson搞笑,搞笑。我的意思是Python标准明确禁止所有导致动态局部变量创建的实践。与此相反,例如,Perl的
    $$
    。实际上,在模块级别,您可以修改
    globals
    返回的dict。在许多实现中(我认为),您可以使用
    局部变量
    做同样的事情,但这一点不能保证。至于将它与
    perl
    进行对比,那么我需要学习perl来理解语法——(我想我明白了)——但我不想学习perl…@mgilson使用
    locals
    ,那样是不安全的。这就是为什么我用“理智”这个词。谢谢你的回答。我已经更新了帖子,这可能会稍微解释一下这个问题……谢谢你的帮助。实际上,这是unittest函数的一部分。我无法将外部参数发送到unittest。我提出的唯一解决方案是使用全局变量。。。