Image of steampunk gears used to represent Python functions

Using Positional Arguments and Named or Keyword Arguments in Python Functions [Intermediate Python Functions Series #2]

In the first article in this Intermediate Python Functions Series, you made sure you got your terminology right. So you know what an argument is. But what are positional arguments and keyword arguments in Python?

In this article, you’ll see how you can choose to use either of the two in many situations. Later in the series, you’ll see when you can’t choose!

Overview Of The Intermediate Python Functions Series

Here’s an overview of the seven articles in this series:

  1. Introduction to the series: Do you know all your functions terminology well?
  2. [This article] Choosing whether to use positional or keyword arguments when calling a function
  3. Using optional arguments by including default values when defining a function
  4. Using any number of optional positional and keyword arguments: *args and **kwargs
  5. Using positional-only arguments and keyword-only arguments: the “rogue” forward slash / or asterisk * in function signatures
  6. Type hinting in functions
  7. Best practices when defining and using functions

Positional Arguments and Keyword (Named) Parameters

Let’s have a look at the following function and the function calls beneath it:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 1.
greet_person("Elizabeth", 5)

# 2.
greet_person("Ishaan", number=3)

# 3.
greet_person(person="Stephen", 10)

# 4.
greet_person(person="Stephen", number=10)

# 5.
greet_person(number=10, person="Stephen")

Before you read on, can you look at the five function calls and try to guess which ones will work and which won’t?

The function greet_person() has two parameters:

  • person
  • number

All five function calls have two arguments: a string and an integer.

Let’s look at all five function calls one at a time.

1.

greet_person("Elizabeth", 5)

This is probably the most commonly used type of function call. The two arguments are both positional arguments. This means that the values "Elizabeth" and 5 are matched to the parameter names person and number depending on their position in the function call.

  • The first argument, "Elizabeth", is assigned to the first parameter name, person
  • The second argument, 5, is assigned to the second parameter name, number

This works fine:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 1.
greet_person("Elizabeth", 5)

Output:

Hello Elizabeth! How are you doing today?
Hello Elizabeth! How are you doing today?
Hello Elizabeth! How are you doing today?
Hello Elizabeth! How are you doing today?
Hello Elizabeth! How are you doing today?

2.

greet_person("Ishaan", number=3)

In this function call, the first argument, "Ishaan", is a positional argument as in the first example.

However, the second argument is a named argument or a keyword argument. You’ll see both terms used, so it’s good to be familiar with both!

The value 3 is matched to a parameter by naming the parameter. In this case, you have number=3 in the function call. This parameter name is often called a keyword in this context, which is why you can refer to this argument as a named or keyword argument.

This means that in this function call, you have one positional argument and one keyword (named) argument. This call works perfectly fine, too:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 2.
greet_person("Ishaan", number=3)

Output:

Hello Ishaan! How are you doing today?
Hello Ishaan! How are you doing today?
Hello Ishaan! How are you doing today?

3.

greet_person(person="Stephen", 10)

This function call seems identical to the case in the second example since there is one positional argument and one keyword argument. However, we’re breaking one of the rules about using keyword and positional arguments. Let’s look at the SyntaxError we get before discussing why we get this error:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 3.
greet_person(person="Stephen", 10)

Output:

File "...", line 6
    greet_person(person="Stephen", 10)
                                     ^
SyntaxError: positional argument follows keyword argument

Here’s the rule: When using a mixture of positional and keyword arguments, the positional arguments must come first.

And this rule makes perfect sense since Python relies on the position of the positional argument within the function call to know which parameter name to assign it to. You can’t change the position of a positional argument!

4.

greet_person(person="Stephen", number=10)

In this case, you’ve used both arguments as named (or keyword) arguments. You’re no longer relying on the position of either of the arguments. What matters now is the keyword you use with each argument when calling the function.

This works fine:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 4.
greet_person(person="Stephen", number=10)

Output:

Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?

This leads us nicely to the fifth and final function call…

5.

greet_person(number=10, person="Stephen")

Did you spot the difference? The arguments are in a different order to the parameters in the function signature. However, both arguments are keyword (named) arguments. Does this matter? Let’s find out:

def greet_person(person, number):
    for greeting in range(number):
        print(f"Hello {person}! How are you doing today?")

# 5.
greet_person(number=10, person="Stephen")

Output:

Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?
Hello Stephen! How are you doing today?

This works perfectly fine. Since you’re using these arguments as named arguments, you no longer need to stick to the same order that’s used in the function definition. Python no longer uses the position to assign the arguments to the parameters. Instead, it’s using the keyword.

This method is particularly useful in functions with many parameters since you don’t need to use them in a particular order when calling the function.

Final Words

In this type of function definition, the programmer calling this function has a choice on whether to use positional arguments, named (keyword) arguments, or a mixture of both positional and named. However, if using both positional arguments and named (keyword) arguments, the positional ones must come first.

Later in this series, you’ll see function definitions which force the programmer to use positional-only or keyword-only arguments.

In summary:

  • Arguments can be positional arguments or named (keyword) arguments
  • When using positional arguments, the arguments are matched to parameter names depending on their position
  • Named (keyword) arguments include the parameter name followed by an equals = before the argument in the function call

Next Article: Optional Arguments With Default Values in Python Functions

Further Reading


Get the latest blog updates

No spam promise. You’ll get an email when a new blog post is published


3 thoughts on “Using Positional Arguments and Named or Keyword Arguments in Python Functions [Intermediate Python Functions Series #2]”

Leave a Reply