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:
- 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, …
Python functions are first-class objects. In the example below, we are assigning a function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
# Python program to illustrate functions can be treated as objects
def shout(text):
return text.upper()
print shout('Hello')
yell = shout
print yell('Hello') |
Output
HELLO
HELLO