Callable and Functions in Python
Functions Are First-Class Objects
In Python, functions are treated like any other object.
This means functions can: - be stored in variables - be passed as arguments - be returned from other functions - be stored inside lists/dictionaries
Function Object vs Function Call
This is one of the most important concepts in functional programming.
Function Object
def greet():
return "Hello"
print(greet)
Output
<function greet at 0x...>
Here we are referencing the function itself.
No execution happens.
Function Call
def greet():
return "Hello"
print(greet())
Output
Hello
The parentheses () execute the function.
Visual Difference
| Code | Meaning |
|---|---|
| greet | Function object |
| greet() | Function call / execution |
Storing Functions in Variables
def fireball():
return "Fireball cast!"
spell = fireball
print(spell())
Output
Fireball cast!
The variable stores the function itself.
Passing Functions as Arguments
Functions can be passed into other functions.
Example
def greet(name):
return f"Hello {name}"
def execute(func, value):
return func(value)
print(execute(greet, "Sara"))
Output
Hello Sara
Why Is This Powerful?
This allows: - reusable logic - decorators - callbacks - flexible systems - functional programming patterns
Returning Functions
Functions can also return other functions.
Example
def multiplier(x):
def inner(y):
return x * y
return inner
double = multiplier(2)
print(double(5))
Output
10
Visual Representation
multiplier(2)
│
└── returns inner()
│
└── remembers x = 2
This is the basis of closures.
What Is Callable?
Callable is a type hint used for functions.
It comes from:
from collections.abc import Callable
Basic Callable Example
from collections.abc import Callable
def execute(func: Callable, value: int) -> int:
return func(value)
Callable With Parameters and Return Types
You can specify: - argument types - return type
Syntax
Callable[[arg1_type, arg2_type], return_type]
Example
from collections.abc import Callable
def operation(func: Callable[[int, int], int]) -> int:
return func(2, 3)
What Does callable() Do?
callable() checks if an object can be called like a function.
Example
def greet():
pass
print(callable(greet))
print(callable(42))
Output
True
False
Objects Can Also Be Callable
Classes using __call__() become callable.
Example
class Spell:
def __call__(self):
return "Magic!"
spell = Spell()
print(callable(spell))
print(spell())
Output
True
Magic!
Functions Inside Data Structures
Functions can be stored inside: - lists - tuples - dictionaries
Example
def fire():
return "Fire"
def ice():
return "Ice"
spells = {
"fire": fire,
"ice": ice
}
print(spells["fire"]())
Output
Fire
Common Mistakes
Returning a Function Call Instead of the Function
Wrong:
return greet()
Correct:
return greet
Forgetting Parentheses
Wrong:
print(greet)
Correct:
print(greet())
Real Use Cases
These concepts are heavily used in: - decorators - callbacks - event systems - retry systems - Flask/FastAPI routes - functional programming - ML libraries
Key Takeaways
- Functions are first-class objects in Python
- Functions can be passed, stored, and returned
Callableis used for function type hintscallable()checks if an object can be executedfunctionandfunction()are very different- Closures and decorators rely heavily on these concepts