Skip to content
Home » Functions in Python – Arbitrary Number of Both Positional and Keyword Parameters

Functions in Python – Arbitrary Number of Both Positional and Keyword Parameters

Spread the love

Here’s another article in the Functions in Python series. If you haven’t read the previous parts yet, feel free to do so. Here they are:

1) Introduction to Functions

2) Functions with Parameters

3) The return Statement

4) Mandatory Parameters

5) Optional Parameters

6) Mutable Optional Parameters

7) Keyword Arguments

8) Arbitrary Number of Positional Parameters

9) Arbitrary Number of Keyword Parameters

Today we’ll learn how to pass an arbitrary number of both positional and keyword parameters to a function.

If we need both * and ** arguments, the keyword arguments (**) come last:

def company_info(company_name, *cities, **workers):
    print(company_name)
    print()
    if len(cities) > 0:
        print("Our company is based in", cities[0])
    if len(cities) > 1:
        print("We have factories in:")
        print()
        for city in cities:
            print(city)
    if len(workers) > 0:
        print()
        print("Our workers:")
        print()
        for worker in workers:
            print("{} - {}".format(worker.replace("_", " "), workers[worker]))

company_info("Breadmasters", "New York", "Chicago", "Sacramento", "Atlanta", Mike_Harrison = "baker", Tom_Wilkins = "baker", Sue_Earnest = "manager")

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

And here’s the output:

Breadmasters

Our company is based in New York
We have factories in:

New York
Chicago
Sacramento
Atlanta

Our workers:

Mike Harrison - baker
Tom Wilkins - baker
Sue Earnest - manager

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

If we try to run the program with different parameters, we’ll get other results. Here are some examples:

– with one * argument and multiple ** arguments

company_info("Breadmasters", "New York", Mike_Harrison = "baker", Tom_Wilkins = "baker", Sue_Earnest = "manager")

The output:

Breadmasters

Our company is based in New York

Our workers:

Mike Harrison - baker
Tom Wilkins - baker
Sue Earnest - manager

– with no * arguments and multiple ** arguments:

company_info("Breadmasters", Mike_Harrison = "baker", Tom_Wilkins = "baker", Sue_Earnest = "manager")

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

The output:

Breadmasters


Our workers:

Mike Harrison - baker
Tom Wilkins - baker
Sue Earnest - manager

– with multiple * arguments and no ** arguments:

company_info("Breadmasters", "New York", "Chicago", "Sacramento", "Atlanta")

The output:

Breadmasters

Our company is based in New York
We also have factories in:

New York
Chicago
Sacramento
Atlanta

– with just one argument (the mandatory one):

company_info("Breadmasters")

The output:

Breadmasters

If the number of parameters is not fixed, we say the function has indefinite arity. Such functions are sometimes called variadic functions.

In Python we often use the *args and **kwargs notation. So, if we have a function that may take any number of positional and keyword arguments, we write it like so:

our_function(*args, **kwargs)

Spread the love

Leave a Reply