

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can a Python function return a function?
Python supports first-class functions. In fact, all functions in python are first-class functions.
Python may return functions from functions, store functions in collections such as lists and generally treat them as you would any variable or object.
Defining functions in other functions and returning functions are all possible.
The given code is re-worked as follows. We define functions inside functions and return these.
Example
def f2(c, d): return c, d def f1(a, b): c = a + 1 d = b + 2 return lambda: f2(c,d) result = f1(1, 2) print result print result()
Output
C:/Users/TutorialsPoint1/~.py <function <lambda> at 0x0000000003041CF8> (2, 4)
- Related Questions & Answers
- How can we return a dictionary from a Python function?
- How can we return a tuple from a Python function?
- How can we return a list from a Python function?
- How can we overload a Python function?
- How to return a json object from a Python function?
- How can we return multiple values from a function in C#?
- How to return an object from a function in Python?
- How to return a JSON object from a Python function in Tkinter?
- How to have a function return a figure in Python (using Matplotlib)?
- How to return a value from a JavaScript function?
- How to return a string from a JavaScript function?
- How can we return multiple values from a function in C/C++?
- How can we define a Python function at runtime?
- How to return none from Python function?
- How to return void from Python function?
Advertisements