Advertisement
Beginner Python, Functions & Scope

What does it mean that functions are 'first-class objects' in Python?

Short answer

Functions can be assigned to variables, passed as arguments to other functions, returned from functions, and stored in data structures like lists or dicts -- treated just like any other value (an int or a string) rather than being a special, restricted kind of construct.

Save Python mock interview
Advertisement
AModel answer
Functions can be assigned to variables, passed as arguments to other functions, returned from functions, and stored in data structures like lists or dicts -- treated just like any other value (an int or a string) rather than being a special, restricted kind of construct.
def greet():
    return "Hello!"

say_hello = greet          # assigned to a variable
functions = [greet, print]  # stored in a list

def call_it(func):           # passed as an argument
    return func()

print(call_it(greet))  # "Hello!"
Real-world example
Passing a specific comparison or transformation function as an argument to a generic sorting or processing utility.
Likely follow-ups
  • How does this first-class function support directly enable Python's decorator syntax?
Related topics