Home > OS >  How to pass parameters to functions inside of fuctions
How to pass parameters to functions inside of fuctions

Time:10-28

There is something in Python that has been bugging me for a long time. I can't figure out how to pass parameters from one function to the functions that are defined inside of that function.

def func1(arg1):
    def func2(arg1):
        print(arg1)
    func2()
var1 = 123
func1(var1)

Here func1 and func2 should have the same parameters but don't.

CodePudding user response:

You have missed the argument in the call of func2:

def func1(arg1):

    def func2(arg1):
        print(arg1)

    # ---> here you have missed the argument
    func2(arg1)

var1 = 123
func1(var1)

CodePudding user response:

Can't you use it like this?

def func1(arg1):
    def func2(): <-- Removed parameter 
        print(arg1)
    func2()
var1 = 123
func1(var1)

Because when you are calling func2 inside func1, the arg1 in func2 is undefined since you passed no parameters; you should read about global and local variables in programming.

  • Related