Open In App

Python Inner Functions

In Python, functions are treated as first class objects. First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.

Properties of first class functions:



Note: To know more about first class objects click here.

Inner functions

A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function. This process is also known as Encapsulation. To know more about encapsulation click here.



Example:




# Python program to illustrate 
# nested functions 
def outerFunction(text): 
    text = text 
    
    def innerFunction(): 
        print(text) 
    
    innerFunction() 
    
if __name__ == '__main__'
    outerFunction('Hey !'

Output:

Hey!

In the above example, innerFunction() has been defined inside outerFunction(), making it an inner function. To call innerFunction(), we must first call outerFunction(). The outerFunction() will then go ahead and call innerFunction() as it has been defined inside it.

It is important that outer function has to be called, so that the inner function can execute. To demonstrate this consider the below example:
Example:




# Python program to illustrate 
# nested functions 
def outerFunction(text): 
    text = text 
    
    def innerFunction(): 
        print(text) 
    
    innerFunction() 

Output:

This code will return nothing when executed.

Scope of variable in nested function

The location where we can find a variable and also access it if required is called the scope of a variable.
It is known how to access a global variable inside a function, but, what about accessing the variable of an outer function? Let’s see an example:




# Python program to 
# demonstrate accessing of
# variables of nested functions
  
def f1():
    s = 'I love GeeksforGeeks'
      
    def f2():
        print(s)
          
    f2()
  
# Driver's code
f1()

Output:

I love GeeksforGeeks

In the above example, it can be seen that it is similar to accessing the global variable from a function. Now let’s suppose you want to change the variable of the outer function.




# Python program to 
# demonstrate accessing of
# variables of nested functions
  
def f1():
    s = 'I love GeeksforGeeks'
      
    def f2():
        s = 'Me too'
        print(s)
          
    f2()
    print(s)
  
# Driver's code
f1()

Output:

Me too
I love GeeksforGeeks

It can be seen the value of the variable of the outer function is not changed. However, the value of the variable of the outer function can be changed. There are different ways to change the value of the variable of the outer function.

Python Closures

A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory.




# Python program to illustrate 
# closures 
def outerFunction(text): 
    text = text 
    
    def innerFunction(): 
        print(text) 
    
    return innerFunction # Note we are returning function WITHOUT parenthesis 
    
if __name__ == '__main__'
    myFunction = outerFunction('Hey !'
    myFunction() 

Output:

Hey!




# Python program to illustrate 
# closures 
import logging 
logging.basicConfig(filename ='example.log', level = logging.INFO) 
    
    
def logger(func): 
    def log_func(*args): 
        logging.info( 
            'Running "{}" with arguments {}'.format(func.__name__, args)) 
        print(func(*args)) 
    # Necessary for closure to work (returning WITHOUT parenthesis) 
    return log_func               
    
def add(x, y): 
    return x +
    
def sub(x, y): 
    return x-
    
add_logger = logger(add) 
sub_logger = logger(sub) 
    
add_logger(3, 3
add_logger(4, 5
    
sub_logger(10, 5
sub_logger(20, 10

Output:

6
9
5
10

Article Tags :