First-Class Functions
Pass and return functions.
Functions are objects
In Python, functions are first-class objects. They can be assigned to variables, stored in data structures, passed as arguments, and returned from other functions — just like numbers or strings.
def greet():
return 'hi'
print(type(greet))
print(greet())Assigning functions to variables
A function name is just a reference. Assign it to another name and call through that name — no parentheses when referring to the function itself.
def shout(text):
return text.upper()
yell = shout
print(yell('hello'))All lessons in this course
- First-Class Functions
- Closures and Free Variables
- nonlocal and Mutable State
- Partial Functions