How do nested functions work in Python?

PythonServer Side ProgrammingProgramming

To learn about nested function, refer the following code. In the code, you can see Inner functions can access variables from the enclosing scope, which is the local variable.

def mulFunc(num1):
   def mul(num2):
      return num1 * num2
   return mul
res = mulFunc(15)
// The following prints 300 i.e. 20*15
print(res(20))

The above prints the multiplication of num1 and num 2 i.e. 300

raja
Published on 24-Jan-2018 07:04:26
Advertisements