How do I pass a method as a parameter in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, functions are first-class objects — they can be assigned to variables, stored in data structures, and passed as arguments to other functions. To pass a function as a parameter, simply use its name without parentheses. Parentheses call the function; without them, you pass the function object itself. This is the foundation for callbacks, higher-order functions, decorators, and strategy patterns in Python.
Basic Example
greet (no parentheses) is the function object. greet("Alice") calls the function and passes the return value.
Function References vs Function Calls
Passing Built-In Functions
Passing Lambda Functions
Passing Methods (Bound and Unbound)
Callback Pattern
Higher-Order Functions
A higher-order function takes a function as input or returns a function as output:
Type Hints for Function Parameters
Common Pitfalls
- Calling the function instead of passing it:
apply(greet("Alice"))callsgreetimmediately and passes the return value. Useapply(greet)to pass the function object. The distinction is parentheses:funcis the object,func()is a call. - Lambda limitations: Lambdas can only contain a single expression, not statements. You cannot use
if/elseblocks, assignments, orprintin a lambda. For complex logic, define a named function withdef. - Mutating default arguments in passed functions: If a function parameter has a mutable default (like
def f(lst=[])), the default is shared across all calls. This is a Python gotcha unrelated to passing functions but often surfaces in callback patterns. - Losing
selfcontext with unbound methods: PassingCalculator.addinstead ofcalc.addloses the instance reference. The unbound method requiresselfas the first argument. Use bound methods (instance.method) for callbacks that need instance state. - Not using
functools.partialfor pre-filling arguments: If you need to pass a function with some arguments pre-filled, usefunctools.partialinstead of wrapping in a lambda:partial(add, 5)instead oflambda x: add(5, x).
Summary
- Pass functions by name without parentheses:
funcnotfunc() - Built-in functions (
len,str.upper,abs) can be passed directly tosorted,map,filter - Use
lambdafor short anonymous functions - Bound methods (
obj.method) carry the instance; unbound methods (Class.method) do not - Use
Callable[[arg_types], return_type]for type hints on function parameters - Use
functools.partialto pre-fill arguments on passed functions
Related reading
- How do I pass a string into subprocess.Popen using the stdin argument?
- How do I pass an async function to a thread target in Python?
- How do I plot in real-time in a while loop?
- How do I prepend to a short python list?
- How do I prevent Conda from activating the base environment by default?
- How do I print an exception in Python?
- How do I print an exception in Python?
- How do I print the full NumPy array, without truncation?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.