简体   繁体   中英

passing argument of outer function as argument for inner function in python?

i want to use multiple functions via one outer function. the argument is a string and upon that string i want to create/pass arguments for inner functions. 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. Functions automatically have access to values from the enclosing 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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