Passing Parameters to a Thread Target in Python
When working with multithreading in Python, a common question is whether you can pass parameters to the function executed by a thread. The short answer is yes — Python’s threading. Thread class provides built-in support for this.
This post walks through the correct and idiomatic ways to pass arguments to a thread target function.
The Basic Thread Pattern
A typical thread is created like this:
import threading
t = threading.Thread(target=compare_totals)
t.start()
This works only if compare_totals takes no parameters. If your function requires inputs, you must supply them explicitly.
Passing Positional Arguments with args
Use the args parameter to pass positional arguments to the target function. args must be a tuple.
def compare_totals(source, target):
print(source, target)
t = threading.Thread(
target=compare_totals,
args=(“athena”, “oracle”)
)
t.start()
Each element in the tuple maps to a parameter in the function signature.
Passing Keyword Arguments with kwargs
If you prefer named arguments (or want clearer intent), use kwargs:
def compare_totals(source, target):
print(source, target)
t = threading.Thread(
target=compare_totals,
kwargs={
“source”: “athena”,
“target”: “oracle”
}
)
t.start()
This approach is especially helpful when a function takes many parameters or optional values.
Passing a Single Object (Such as a Dictionary)
A common pattern is to pass a single dictionary containing multiple configuration values:
def compare_totals(params):
print(params)
params = {
“engine”: “mysql”,
“schema”: “public”,
“table”: “employees”
}
t = threading.Thread(
target=compare_totals,
args=(params,)
)
t.start()
⚠️ Important:
When passing a single argument via args, you must include a trailing comma: (params,). Without it, Python will not treat the value as a tuple.
Summary
- Use args for positional arguments
- Use kwargs for named arguments
- Always pass args as a tuple, even for a single value
- Thread targets behave just like normal function calls — the thread simply invokes the function with the supplied parameters
Understanding this pattern makes it much easier to parallelize work cleanly and safely in Python.


