python-course.eu

26. Parameters And Arguments

By Bernd Klein. Last modified: 10 Nov 2023.

Introduction

parameter wood

At first the definition and usage of Python parameters and arguments seems to be simple, but there exists a multitude of intricate details that can be perplexing. So that you don't get lost in the "parameter wood", we provide this introduction.

Let's start with a simple question: If you look at the following code, is everything clear to you? If so, you don't have to continue reading this chapter of our Python tutorial. If not, you should definitely continue reading.

def all_together_now(a, /, b, c=11, d=12, *args, kw_only1=42, kw_only2=84, **kwargs):
    return f'{a=}, {b=}, {c=}, {d=}, {args=}, {kw_only1=}, {kw_only2=}, {kwargs=}'
    

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here

Required Parameters

Let's look first at a function with one parameter which can or better has be called with a positional or keyword argument:

def positional_example(x):
    return x + 42

We can call this function with a numerical value like this:

positional_example(1)

OUTPUT:

43

We can also call it with a keyword argument, i.e. by assigning the object explicitly to the parameter name:

positional_example(x=1)

OUTPUT:

43

Positional Only Parameters

Positional-only parameters have no externally-usable name. This implies that you cannot assign an object to the parameter's name when making a function call. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order.

Positional-only Parameters are a way to prevent using arguments as keyword arguments. We demonstrate this in the following example. All the parameters (in our example only the parameter x) before the '/' are defined as positional only arguments. The parameters following the '/' can be used both positionally and as keyword arguments:

def positional_only_example(x, /, y, z):
    return x + 42
positional_only_example(1, 4, 6)

OUTPUT:

43

As we mentioned before, we can use y and z keyword arguments:

positional_only_example(1, z=6, y=4)

OUTPUT:

43

If you try to use x as a keyword argument

positional_only_example(x=1, z=6, y=4)

m you will get the exception

TypeError: positional_only_example() got some positional-only arguments passed as keyword arguments: 'x'

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Upcoming online Courses

Enrol here

Optional Positional Only Parameters

This is the same as before, but the function can also be called without giving an argument:

def optional_positional_only_example(x=42, /):
    return x + 42

print(optional_positional_only_example(1))
# or without an argument:
print(optional_positional_only_example())

OUTPUT:

43
84

Keyword-only Parameter

So, we have seen parameters which can only be used in a call as positional but not as keyword parameters. What about parameters that must be utilized as keyword arguments and cannot be employed as positional arguments?

def keyword_only_parameters(*, key):
    return key

keyword_only_parameters(key=99)

OUTPUT:

99

But you cannot call keyword_only_parameters' in a positional way. If you call it with keyword_only_parameters(42)` you will get the following exception: TypeError: keyword_only_parameters() takes 0 positional arguments but 1 was given

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here

Optional Keyword-only Parameter

Like before, but if the keyword is not given in the function call the default value will be uses:

def keyword_only_parameters(*, key=42):
    return key

print(keyword_only_parameters(key=99))
print(keyword_only_parameters())

OUTPUT:

99
42

Keyword-only Parameters after *args

It's easy to imagine a function that accepts a variable number of arguments along with one or more 'options' provided as keyword arguments. There is a way to add these options after the arbitrary list of optional positional parameters.

def keyword_only_args(x, *args, a, b):
    return x, args, a, b

print(keyword_only_args("pos", "arg1", "arg2", "arg3", a=3, b=4))

OUTPUT:

('pos', ('arg1', 'arg2', 'arg3'), 3, 4)

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Enrol here

All Together Now

def all_together_now(a, /, b, c=11, d=12, *args, kw_only1=42, kw_only2=84, **kwargs):
    return f'{a=}, {b=}, {c=}, {d=}, {args=}, {kw_only1=}, {kw_only2=}, {kwargs=}'
all_together_now(2, 4)

OUTPUT:

'a=2, b=4, c=11, d=12, args=(), kw_only1=42, kw_only2=84, kwargs={}'
all_together_now(2, name='Guido', age=42, kw_only1=99, b=1001)

OUTPUT:

"a=2, b=1001, c=11, d=12, args=(), kw_only1=99, kw_only2=84, kwargs={'name': 'Guido', 'age': 42}"

all kinds of parameters in one function

Keyword Arguments vs. Optional Parameters

"Keyword argument" is like the term "argument" implies related to a function call and not in the function definition. Quite often people confuse optional parameters in a function definition with keyword parameters.

def foobar1(optional_parameter1=42, optional_parameter2=22):
    return optional_parameter1 + optional_parameter2
def foobar2(positional_parameter1, positional_parameter2):
    return positional_parameter1 + positional_parameter2

Both functions can be called with positional or keyword parameters, as you can see in the following Python function calls:

print(foobar1(1, 99))
print(foobar1(1, optional_parameter2=99))
print(foobar1(optional_parameter1=1, optional_parameter2=99))

OUTPUT:

100
100
100

Now, the same way of calling with foobar2:

print(foobar2(1, 99))
print(foobar2(1, positional_parameter2=99))
print(foobar2(positional_parameter1=1, positional_parameter2=99))

OUTPUT:

100
100
100

The distinction between these two functions is rooted in the flexibility of their argument handling. foobar1 can be invoked with no arguments, a single argument, or both arguments. This flexibility arises from the default values provided in the function definition, which can be utilized if a parameter is absent. In contrast, when using foobar2, it is mandatory to provide values for both arguments every time it's called, without any option to rely on defaults.

print(foobar1())
print(foobar1(1))
print(foobar1(optional_parameter2=6))
print(foobar1(1, 99))

OUTPUT:

64
23
48
100

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Upcoming online Courses

Enrol here