Python Argument Syntax
February 2, 2017
This article is an overview of how arguments can be defined and used with Python callables. The general term callables means something that can be called like a function, method, or object.
An argument is the value passed into a callable (function or method). The function should consume these values in the function body.
There are two types of arguments:
Positional argument - a single value: my_function(42)
the 42
is the positional argument.
Keyword argument - are identified by a name:
my_function(size='large')
the keyword argument has a name and value size='large'
There are two ways arguments are used:
Formal arguments - arguments defined in the function definition
Actual arguments - arguments supplied at the function call site
Arguments can be expressed in multiple ways:
arg
- a single argument value
*args
- a series of argument values
**kwargs
- a series of keyword argument values
For arbitrary argument lists, the use of *
and **
is the syntax
to use and the names args
and kwargs
is only a convention used
and not required. You can use your own names if it makes your code more
explicit, for example: *points
or **teams
for sports-ball.
The *args
and **kwargs
are used to consume arbitrary lists of
arguments of unknow length. Like a catch-all of extra arguments passed into a
function or method.
Formal Arguments
The order of arguments is important. Positional arguments will be first, then
star args (*args
), and last is keyword args (**kwargs
).
The best way to understand this is with examples.
My functions below are to demonstrate how the function interprets the arguments, and not what the function actually does. The example functions print the arguments sent into them.
Example:
Two positional arguments the function requires, then an unknow list of
arguments.
# Function definition.
def print_args(arg1, arg2, *args):
print(arg1)
print(arg2)
print(args)
# Function being called.
print_args(1,2,3,4,5)
# Function output.
1
2
(3, 4, 5) # *args
Example
Using star args (*args
) to consume an unkown length of arguments. The
arguments will be a tuple available in the function.
# Function definition.
def print_args(*args):
print(args)
print(type(args))
# Function being called.
print_args(2,5)
# Function output.
(2, 5)
<class tuple>
# Function being called.
print_args(1)
# Function output.
(1,)
<class tuple>
Example
Keyword arguments being used after star args (*args
).
# Function definition.
def print_args(arg1, arg2, *args, kwarg1, kwarg2):
print(arg1)
print(arg2)
print(args)
print(kwarg1)
print(kwarg2)
# Function being called.
print_args(1,2,3,4,5,6, kwarg1=7, kwarg2=8)
# Function output.
1
2
(3, 4, 5, 6) # *args
7 # kwarg1
8 # kwarg2
Example Using keyword args allows for named arguments and will be a dictionary available in the function body.
# Function definition.
def named_args(**kwargs):
print(kwargs)
# Function being called.
named_args(one="1", two="2", three="3")
# Function output.
{'one': '1', 'three': '3', 'two': '2'}
Example Arbitrary keyword arguments will be the last argument and will be a dictionary that is available in the function body.
# Function definition.
def print_args(arg1, arg2, *args, kwarg1, kwarg2, **kwargs):
print(arg1)
print(arg2)
print(args)
print(kwarg1)
print(kwarg2)
print(kwargs)
# Function being called.
print_args(1,2,3,4,5,6, kwarg1=7, kwarg2=8, kwarg3=9, kwarg4=10)
# Function output.
1
2
(3, 4, 5, 6) # *args
7 # kwarg1
8 # kwarg2
{'kwarg4': 10, 'kwarg3': 9}
Actual Arguments
The *
at the call site is used a little different and does not have the same syntax as the function definition.
The asterisk prefix instructs Python to unpack the series (list, dict, tuple, etc.) when the argument data is sent into the function.
Example
A single *
can be used at the call site for a series of arguments.
# Function definition.
def print_args(arg1, arg2, *args):
print(arg1)
print(arg2)
print(args)
# Data structure that needs to be sent to the function.
t = (1,2,3,4,5,6)
# Function being called with * to pack arguments.
print_args(*t)
# Function output.
1
2
(3, 4, 5, 6) # *args
Example
Using two asterisks **
you can pack keyword arguments (name=value)
and send them into the function.
# Function definition.
def order_info(quantity, size, **kwargs):
print('quantity =', quantity)
print('size =', size)
print(kwargs)
# Data structure that needs to be sent to the function.
order = {'quantity':11, 'size':9, 'customer_id':112}
# Function being called with ** to pack arguments.
order_info(**order)
# Function output.
quantity = 11
size = 9
{'customer_id': 112}
Default values
You can provide a default value for an argument in the function definition.
This will allow the caller of the function to optionally provide a value. If they don’t provide a value, the default will be used.
# Function definition.
def customer_report(customer_id, products=None):
pass
# Function being used without a products value.
customer_report(112)
# Function being used with an optional products value.
customer_report(112, ['shoes', 'pants'])
Parameter vs. Argument
The terms parameter and argument are sometimes used interchangably, but there is a technical difference.
The parameter is the name used in the function definition and the argument is the value passed into the function from the call site.
The function examples in this article are simple functions with no real functionality. Their purpose is to display how the arguments are handled in the function body. The examples also can be applied to arguments passed to methods.
I wrote this article as an overview of basic argument syntax to help beginner programmers and is not an exhaustive list of all argument possibilities.