Difference between Required, Optional, Positional and Keyword arguments

Difference between Required, Optional, Positional and Keyword arguments

If you are like me and terms like required arguments, optional arguments, positional arguments, and keyword arguments have ever left you puzzled, worry not! In this straightforward exploration, we'll break down the complexity and make Python function argument terms easy to understand.

Let's Get Started!

Required vs. Optional Arguments: Relevant while defining a Function

When you're creating a function using def, you'll be able to define two kinds of arguments: required and optional.

  • Required Arguments: Think of these as must-haves for your function. They're like essential guests at a party – they have to be there!

  • Optional Arguments: These are more like the laid-back attendees who can join if they want. You can make an ordinary argument optional by giving it a default value during function creation.

def party_guest_list(required_guest, optional_guest="Chill Attendee"):
    print("Required Guest:", required_guest)
    print("Optional Guest:", optional_guest)

# Passing only the Required Argument
party_guest_list("VIP Guest")

# Passing Required and Optional Arguments
party_guest_list("VIP Guest", "Random Attendee")

Here the required_guest argument is a required argument and optional_guest is an optional one since there is already a default value(Chill Attendee) assigned to it.

Tip: If you spot an argument with arg=value in the function definition, it's not a keyword argument but a cool optional parameter that already has a default value.

Positional vs. Keyword Arguments: Relevant while calling a Function

When you are calling a function, there are two ways in which you can pass the parameters:

  • Positional Arguments: You can pass the parameters based on their position in the function definition. Think of this as following a set order, like a to-do list. The order you pass them must match the function's definition.

  • Keyword Arguments: Here, you can use the name of your parameters and throw them in any order using kwarg=value syntax.

# Calling with Positional Arguments only
party_guest_list("VIP Guest")

# Calling with one Positional and one Keyword
party_guest_list("VIP Guest", optional_guest="Random Attendee")

# Calling with Keyword arguments only
party_guest_list(optional_guest="Random Attendee", required_guest="VIP Guest")

You can pass both required and optional arguments during the function call, utilizing either the positional or keyword approach.

def party_guest_list(required_guest, optional_guest="Chill Attendee"):
    print("Required Guest:", required_guest)
    print("Optional Guest:", optional_guest)
    print("\n")

# Calling only the Required argument with Positional approach
party_guest_list("VIP Guest")

# Calling only the Required argument with Keyword approach
party_guest_list(required_guest="VIP Guest")

# Calling Required argument and optional argument with Positional approach
party_guest_list("VIP Guest", "Random Attendee")

# Calling Required argument and optional argument with one Positional and one keyword argument approach
party_guest_list("VIP Guest", optional_guest="Random Attendee")

# Calling Required argument and optional argument with Keyword approach
party_guest_list(optional_guest="Random Attendee", required_guest="VIP Guest")

There is a lot of flexibility in terms of how you can define and pass the arguments for a function in Python, yet there are some essential rules we have to follow:

  1. Mandatory vs. Optional:

    • Required arguments must always be provided during the function call.

    • Optional arguments, however, are gracious – they can be omitted.

  2. Order of Arrival:

    • All non-keyword arguments must precede the keyword arguments in the function call.
  3. No Duplicate Values Allowed:

    • Be cautious not to provide duplicate values for the same argument. This means avoiding the use of both positional and keyword approaches for the same parameter.
  4. The Unknown Territory:

    • It's a no-go to pass a value for an unknown keyword argument. Stick to the parameters defined in the function.

Syntax Clarification: Optional vs. Keyword

While arg=value for optional and kwarg=value for keyword arguments might seem similar, but they serve different purposes. Optional arguments make function calls easier by letting you define a function that can be called with fewer arguments than it is defined to allow. Keyword arguments, on the other hand, offer flexibility in the way you pass parameters to a function while calling it.