pofi
pofi

Reputation: 11

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:

Upvotes: 0

Views: 2479

Answers (2)

theCoder
theCoder

Reputation: 34

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)

Upvotes: 0

Samwise
Samwise

Reputation: 71434

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 '

Upvotes: 3

Related Questions