I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn’t view Python as a functional programming language.1
Guido van Rossum, Python BDFL
Functions in Python are first-class objects. Programming language theorists define a “first-class object” as a program entity that can be:
Created at runtime
Assigned to a variable or element in a data structure
Passed as an argument to a function
Returned as the result of a function
Integers, strings, and dictionaries are other examples of first-class objects in Python—nothing fancy here. But if you came to Python from a language where functions are not first-class citizens, this chapter and the rest of Part III of the book focuses on the implications and practical applications of treating functions as objects.
The term “first-class functions” is widely used as shorthand for “functions as first-class objects.” It’s not perfect because it seems to imply an “elite” among functions. In Python, all functions are first-class.
The console session in Example 5-1 shows that Python functions are objects. Here we create a function, call it, read its
__doc__ attribute, and check that the function object itself is an instance of the