简体   繁体   English

将外部 function 的参数作为内部 function 的参数传递给 python?

[英]passing argument of outer function as argument for inner function in python?

i want to use multiple functions via one outer function.我想通过一个外部 function 使用多种功能。 the argument is a string and upon that string i want to create/pass arguments for inner functions.参数是一个字符串,我想在该字符串上为内部函数创建/传递 arguments。 how is that possible?这怎么可能?

def outer_function(arg1):
    
    arg2 = 'random_text' + str(arg1)
    arg3 = 'random_text' + str(arg1)
    
    def inner_function(arg2,arg3):
        global var
        do_something ...
    return inner_function()

my error i get is:我得到的错误是:

TypeError: inner_function() missing 2 required positional arguments:

Don't use global , and don't specify the variables as arguments if you're not passing them as arguments.不要使用global ,并且不要将变量指定为 arguments 如果您没有将它们作为 arguments 传递。 Functions automatically have access to values from the enclosing scope.函数自动访问封闭 scope 中的值。

def outer_function(arg1):
    arg2 = 'random_text' + str(arg1)
    arg3 = 'random_text' + str(arg1)

    def inner_function():
        return arg2 + arg3

    return inner_function()

>>> outer_function(" foo ")
'random_text foo random_text foo '

You didn't passed arguments or parameters in return inner_funtion() below code can help您没有通过 arguments 或return inner_funtion()下面的代码可以提供帮助

def outer_function(arg1):
    arg2 = 'random_text' + str(arg1)
    arg3 = 'random_text' + str(arg1)
    def inner_function(arg2,arg3):
        global var
        do_something ...
    return inner_function(arg2,arg3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM