Arguments allow you to pass data to functions and methods in Python. There are two types of arguments in Python - positional arguments and keyword arguments. Understanding how to use them correctly is an important skill for any Python developer.
This comprehensive guide will explain positional and keyword arguments in detail, including when and how to use each type. You’ll learn about:
Table of Contents
Open Table of Contents
Defining Positional and Keyword Arguments
A positional argument is an argument that is identified by its position in the function call. For example:
def func(a, b, c):
print(a, b, c)
func(1, 2, 3)
Here 1
gets assigned to a
, 2
gets assigned to b
, and 3
gets assigned to c
. The order matters.
A keyword argument is an argument that is identified by a variable name or keyword when we call the function. For example:
def func(a, b, c):
print(a, b, c)
func(c=3, b=2, a=1)
Now 1
gets assigned to a
, 2
to b
, and 3
to c
by matching the keyword. The order doesn’t matter here.
So in summary:
- Positional arguments match the function parameters by position. Order matters.
- Keyword arguments match parameters by name. Order doesn’t matter.
When to Use Positional vs Keyword Arguments
Use positional arguments when:
- You want to rely on the position of the arguments, not keywords
- You have a fixed small number of required arguments
- The meaning and order of arguments is clear
Use keyword arguments when:
- You have a large number of optional arguments
- You want to specify arguments out of order
- You want to improve readability by explicitly naming variables
- You need to pass the same variable to different functions with different meanings
Here is an example illustrating when keyword arguments help improve readability:
def greet(greeting, name):
print(f"{greeting}, {name}!")
greet("Hello", "John")
# Less readable
greet("John", "Hello")
# More readable with keywords
greet(name="John", greeting="Hello")
So in summary, use positional for mandatory ordered arguments, and keywords when you want more flexibility.
Unpacking Argument Lists
You can unpack sequences like lists and tuples into positional arguments using the * operator. For example:
def add(a, b):
return a + b
my_list = [1, 2]
add(*my_list) # Equivalent to add(1, 2)
And you can unpack dictionaries into keyword arguments using the ** operator:
def func(a, b, c):
print(a, b, c)
my_dict = {'a': 1, 'b': 2, 'c': 3}
func(**my_dict) # Equivalent to func(a=1, b=2, c=3)
So unpacking lets you flexibly call functions without changing the arguments to match the parameters.
Default Argument Values
You can specify default values for positional and keyword arguments in a function definition:
def func(a, b=2, c=3):
print(a, b, c)
func(1) # 1 2 3
func(1, 4) # 1 4 3
func(1, c=5) # 1 2 5
The default value gets used if no argument value is passed during the function call. This makes arguments optional and allows omitting them.
Default values get evaluated at function definition time, not call time. So this can be problematic for mutable objects:
# Danger - mutable default value
def add_list(value, my_list=[]):
my_list.append(value)
return my_list
add_list(1) # [1]
add_list(2) # [1, 2], my_list mutated!
So use immutable objects like None as default values if you want to avoid this:
def add_list(value, my_list=None):
if my_list is None:
my_list = []
my_list.append(value)
return my_list
Variable Length Positional Arguments
You can specify functions that accept an arbitrary number of positional arguments using the *args syntax:
def func(*args):
for a in args:
print(a)
func(1, 2, 3) # Prints 1, 2, 3
The *args parameter collects extra arguments into a tuple. This provides flexibility to call the function with any number of positional arguments.
Within the function, you can iterate over the args tuple, or access args elements by index.
Here is an example of a averaging function using *args:
def average(*args):
return sum(args) / len(args)
average(1, 2) # 1.5
average(1, 3, 4) # 2.67
Keyword-Only Arguments
To force keywords for some arguments, place them after the * parameter:
def func(a, b, *, c, d):
print(a, b, c, d)
func(1, 2, c=3, d=4) # Works
func(1, 2, 3, 4) # Error - c and d are keyword-only
Now c and d can only be passed by keyword, not position.
Keyword-only arguments are useful to:
- Force keywords for important optional params
- Separate positional-only and keyword-only args
- Prevent positional parameters from changing order
For example, Flask uses keyword-only to prevent API changes:
@app.route('/path', methods=['GET', 'POST'])
def view_func():
pass
Common Errors and Best Practices
Here are some common errors to avoid and best practices to follow for arguments in Python:
Do:
- Use explicit keywords for clarity even when order doesn’t matter
- Prefer default values over conditional logic in functions
- Use variable length *args for clean handling of optional positional arguments
- Use keyword-only arguments to force explicit keywords
Avoid:
- Mutable default values that can lead to unexpected effects
- Calling functions with the wrong number or type of arguments
- Depending on parameter order instead of keywords
- Unpacking arguments that don’t match the function signature
Do not:
- Use return values or exceptions for flow control - prefer default values
- Use positional arguments when keyword arguments would be clearer
- Define functions with lots of hard-to-understand positional arguments
Following PEP 8 style and best practices will help you write Python code that is easy to use, understand, and maintain. Mastering arguments in Python unlocks your ability to design flexible and extensible functions.
Conclusion
This guide covered key concepts about positional and keyword arguments in Python. To summarize:
- Positional arguments match parameters by position, keyword arguments by name
- Use positional for fixed ordered params, keywords for flexibility
- You can unpack sequences and dicts into arguments using * and **
- Default values provide optional behavior for arguments
- *args and keyword-only enable advanced argument handling
- Follow best practices to write Pythonic high quality code
Properly using arguments allows you to write reusable and robust functions. You now have a comprehensive understanding of working with arguments in Python. Happy coding!