First-Class Functions
Functions and nested-functions in Python.
We'll cover the following...
Function: A first-class object
A first-class object is an object that can be:
- Created at runtime (assigned to a variable)
- Stored in a data structure
- Passed as an argument to another function
- Returned as a value from another function
Several data types in Python are first-class objects, e.g. int
, string
, etc. But that’s not it. Functions in Python are also first-class objects. Without any further ado, let us run a program demonstrating the nature of functions as first-class objects.
Press + to interact
def hello():return 'Welcome to Educative'message = hello # Assigning function as an objectprint(message())l = ['hi', message, 'bye'] # Storing function in listprint(l)# Returning function from another functiondef greeting():return message()print(greeting()) # Passing function in built-in function
In the above code, in the beginning, we make a ...