[ Web Proxy ]
URL:
Viewing: https://www.python-course.eu/python-tutorial/parameters-and-arguments.php [Back]  [Original]

26. Parameters And Arguments | Python Tutorial | python-course.eu
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

26. Parameters And Arguments

By Bernd Klein. Last modified: 10 Nov 2023.

Introduction

parameter wood [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 [instructor-led training course]

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

See our Python training courses

See our Machine Learning with Python training courses

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 [instructor-led training course]

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

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

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 [instructor-led training course]

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

See our Python training courses

See our Machine Learning with Python training courses

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 [instructor-led training course]

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

See our Python training courses

See our Machine Learning with Python training courses

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 [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 [instructor-led training course]

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

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page