Open In App

Returning a function from a function – Python

Last Updated : 20 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Functions in Python are 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.

Properties of first-class functions:

  • A function is an instance of the Object type.
  • You can store the function in a variable.
  • You can pass the function as a parameter to another function.
  • You can return the function from a function.
  • You can store them in data structures such as hash tables, lists, …

Example 1: Functions without arguments

In this example, the first method is A() and the second method is B(). A() method returns the B() method that is kept as an object with name returned_function and will be used for calling the second method.

Python3




# define two methods
 
# second method that will be returned
# by first method
def B():
    print("Inside the method B.")
     
# first method that return second method
def A():
    print("Inside the method A.")
     
    # return second method
    return B
 
# form a object of first method
# i.e; second method
returned_function = A()
 
# call second method by first method
returned_function()


Output :

Inside the method A.
Inside the method B.

Example 2: Functions with arguments

In this example, the first method is A() and the second method is B(). Here A() method is called which returns the B() method i.e; executes both the functions.

Python3




# define two methods
 
# second method that will be returned
# by first method
def B(st2):
    print("Good " + st2 + ".")
     
# first method that return second method
def A(st1, st2):
    print(st1 + " and ", end = "")
     
    # return second method
    return B(st2)
 
# call first method that do two work:
# 1. execute the body of first method, and
# 2. execute the body of second method as
#    first method return the second method
A("Hello", "Morning")


Output :

Hello and Good Morning.

Example 3: Functions with lambda

In this example, the first method is A() and the second method is unnamed i.e; with lambda. A() method returns the lambda statement method that is kept as an object with the name returned_function and will be used for calling the second method.

Python3




# first method that return second method
def A(u, v):
    w = u + v
    z = u - v
     
    # return second method without name
    return lambda: print(w * z)
 
# form a object of first method
# i.e; second method
returned_function = A(5, 2)
 
# check object
print(returned_function)
 
# call second method by first method
returned_function()


Output :

<function A.<locals>.<lambda> at 0x7f65d8e17158>
21


Previous Article
Next Article

Similar Reads

Python | Returning index of a sorted list
Sort a list in python and then return the index of elements in sorted order. Examples: Input : [2, 3, 1, 4, 5] Output : [2, 0, 1, 3, 4] After sorting list becomes [1, 2, 3, 4, 5] and their index as [2, 0, 1, 3, 4] Input : [6, 4, 7, 8, 1] Output : [4, 1, 0, 2, 3] After sorting the list becomes [1, 4, 6, 7, 8] and their index as [4, 1, 0, 2, 3]. Meth
3 min read
Returning Multiple Values in Python
In Python, we can return multiple values from a function. Following are different ways 1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class. Python Code # A Python program to return multiple # values from a method using class class Test: def __init__(self)
4 min read
Returning distinct rows in SQLAlchemy with SQLite
In this article, we are going to see how to return distinct rows in SQLAlchemy with SQLite in Python. Installation SQLAlchemy is available via pip install package. pip install sqlalchemy However, if you are using flask you can make use of its own implementation of SQLAlchemy. It can be installed using - pip install flask-sqlalchemyCreating Database
3 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Accessing Python Function Variable Outside the Function
In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using "Global". Accessing Function Variab
3 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Practice Tags :