How to call a Nested function: Python nested function Call!

Hello Learners, today we will learn how to call a nested function in Python with this small tutorial.

Just like nested loops or conditions, nested functions are nothing but function within a function i.e. a function defined inside another function.

Python supports First-Class Function which means python treats the functions as objects. You can pass functions as an argument, return them or you can save them in data structures also. You can do it using Python Closures i.e. a function instance enclosed within an enclosing scope.

There is one thing you should take care of, you must have to call the outer function to call the inner function because it’s scope is inside that function.

So without wasting the time let’s jump into the code snippet so you can have a better understanding of the concept.

def func(x):
    print('hello')
    print("{} is of type {}".format(x,(type(x))))
    
    def nested(x):
        print('inside nested function')
        x=int(x)
        print("{} is of type {}".format(x,(type(x))))
    nested(x)
  
func('2')

OUTPUT:

hello
2 is of type <class 'str'>
inside nested function 
2 is of type <class 'int'>

In this code snippet, on calling function func() with value 2 of string type, it starts executing.

On line 9 it calls the nested function within the func() function and then the nested function is executed.

So in this manner, the nested function is called every time we call the func() function automatically because it is called inside the func() function.

The Requirement of Nested Functions: nested function call

Python Closures or you can say nested function objects can be used to protect or filter some functionalities inside that function. These functionalities are protected from outer space or processes which is nothing but Encapsulation. It can be achieved by using nested functions.

Now suppose you don’t want to execute the nested() function all the times you call func(), what will you do now?

def func(x):
    print('hello')
    print("{} is of type {}".format(x,(type(x))))
    
    def nested(x):
        x=int(x)
        print("inner function : {} is of type {}".format(x,(type(x))))
    
    print("{} is of type {}".format(x,(type(x))))
    return nested

f = func('2')
f('2')

OUTPUT: 

hello
2 is of type <class 'str'>
2 is of type <class 'str'>
inner function : 2 is of type <class 'int'>

In this code snippet, the outer function is called on line 12 but the inner function is not. It will be called only when we call this new function named ‘f’  with specified arguments.

On line 13, we called f with the parameter required and we can see the output. The inner function is called and the argument is converted into an integer.

There is another concept of nonlocal keyword which we’ll learn in another tutorial when we’ll discuss the scope of variables. For now, you can see this article for the nonlocal keyword.

Leave a Reply

Your email address will not be published. Required fields are marked *