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, …
Examples illustrating First Class functions in Python
1. Functions are objects: Python functions are first class objects. In the example below, we are assigning 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
2. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which takes a function as an argument.
# Python program to illustrate functions # can be passed as arguments to other functions def shout(text): return text.upper() def whisper(text): return text.lower() def greet(func): # storing the function in a variable greeting = func("Hi, I am created by a function passed as an argument.") print greeting greet(shout) greet(whisper) |
Output
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT. hi, i am created by a function passed as an argument.
3. Functions can return another function: Because functions are objects we can return a function from another function. In the below example, the create_adder function returns adder function.
# Python program to illustrate functions # Functions can return another function def create_adder(x): def adder(y): return x+y return adder add_15 = create_adder(15) print add_15(10) |
25
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Partial Functions in Python
- Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
- Calendar Functions in Python | Set 2(monthrange(), prcal(), weekday()...)
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Array in Python | Set 2 (Important Functions)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Statistical Functions in Python | Set 2 ( Measure of Spread)
- Iterator Functions in Python | Set 1
- Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
- Decimal Functions in Python | Set 1
- Decimal Functions in Python | Set 2 (logical_and(), normalize(), quantize(), rotate() ... )
- Operator Functions in Python | Set 1

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
