Functions as first-class objects
It shouldn't come as a surprise that Python functions are first-class objects. In Python, functions are objects with a number of attributes. The reference manual lists a number of special member names that apply to functions. Since functions are objects with attributes, we can extract the docstring
function or the name of a function, using special attributes such as __doc__
or __name__
. We can also extract the body of the function via the __code__
attribute. In compiled languages, this introspection is relatively complex because of the source information that needs to be retained. In Python, it's quite simple.
We can assign functions to variables, pass functions as arguments, and return functions as values. We can easily use these techniques to write higher-order functions.
Since functions are objects, Python already has many features required to be a functional programming language.
Additionally, a callable object also helps us to create functions...