Keyword only Argument in Python

Two unique argument handling mechanisms exist in the positional-only and the keyword-only argument in Python. These arguments will offer a way for the arguments to pass a function with the help of a function and this will be passed with the keyword. Read this article to learn more about Keyword-only Argument in Python.

What is Keyword Only Argument?

Keyword Arguments refers to a value that will be passed into a function while running the function calls. The two kinds of keyword Arguments are given below:

  1. Keyword Argument

A Keyword Argument is an argument that will be preceded by the identifier. So, this value will be passed through the function call and not through the default value while defining the function parameters.

2. Positional Argument

A Positional argument is referred to as the all argument but is not a keyword argument.

Note:

It is known that function calls will pass arguments either as the Positional or Keyword Arguments. However, this function call will be done according to the needs. If the arguments are necessary for the function call and are a must to be passed on. Therefore, the value will be passed as the Positional Argument.

What is the difference between a Keyword-only Argument and a Positional Argument?

Keyword-Only ArgumentPositional only argument
Arguments are passed in the order of parameters. The order is defined in the order function declaration.The order of parameter Names can be changed to pass the argument(or values).
The order of values cannot be changed to avoid the unexpected output.The order of values cannot be changed to avoid unexpected output.
Syntax of Keyword Only argument isFunctionName(paramName = value, ...)Syntax of Positional only argument isFunctionName(value1, value2, value3,....)

What is Keyword – Only Argument?

The asterisks that are used while defining the function are referred to as the Keyword argument. This argument works similarly to the vanilla Keyword Argument. It will compel the users to force the pass keyword arguments other than the positional or direct arguments.

What is an example of Keyword Only Argument?

Let’s look into the example of Keyword only Argument

def greet(name, greeting="Hello", punctuation="!"):
    """
    Function to greet a person with a customizable message.

    Parameters:
    - name (str): The name of the person to greet.
    - greeting (str): The greeting message (default is "Hello").
    - punctuation (str): The punctuation to use at the end of the greeting (default is "!").

    Returns:
    str: The complete greeting message.
    """
    return f"{greeting}, {name}{punctuation}"

# Example usage with keyword arguments
result1 = greet(name="Alice")
result2 = greet(name="Bob", greeting="Hi")
result3 = greet(name="Charlie", punctuation=".")

print(result1)  # Output: Hello, Alice!
print(result2)  # Output: Hi, Bob!
print(result3)  # Output: Hello, Charlie.

Therefore, use the function provided above as an example. Therefore, this function will make the username with the help of the user’s custom separator. Additionally, it will use the packing arguments.

>>> generate_username("Hello, Alice!", "Hi, Bob!", "Hello, Charlie", separator="_")

Output

Hello, Alice!
Hi, Bob!
Hello, Charlie.

This example has mentioned the ‘greet‘ as a function and it has three parameters. Such as (name, greeting, and punctuation). The ‘greeting ‘ and ‘punctuation’ have a default value that will make them optional. During the function call, it has a value for any parameters with the help of a keyword argument.

What are the Positional Arguments?

Positional Arguments are referred to as the default way of passing arguments into the function.

What is the example of the Positional Argument?

def greet_positional(name, greeting, punctuation):
    """
    Function to greet a person with positional arguments.

    Parameters:
    - name (str): The name of the person to greet.
    - greeting (str): The greeting message.
    - punctuation (str): The punctuation to use at the end of the greeting.

    Returns:
    str: The complete greeting message.
    """
    return f"{greeting}, {name}{punctuation}"

# Example usage with positional arguments
result1 = greet_positional("Alice", "Hello", "!")
result2 = greet_positional("Bob", "Hi", "...")
result3 = greet_positional("Charlie", "Greetings", "?")

print(result1)  # Output: Hello, Alice!
print(result2)  # Output: Hi, Bob...
print(result3)  # Output: Greetings, Charlie?

In the example given above, the greet_positional function has three parameters (name, greeting, and punctuation) as positional arguments. Therefore during the function call, it is possible to provide values in the same order as the parameters, and the function uses them accordingly to generate the greeting message.

Output

Hello, Alice!
Hi, Bob...
Greetings, Charlie?

Hence, each line that corresponds to the result of the calling ‘greet_positional ‘ will function with different sets of positional arguments.

Conclusion

In Python, keyword-only arguments provide a way to make our functions more flexible and readable, especially for beginners. With keyword-only arguments, we can assign values to specific parameters by explicitly mentioning their names when calling a function.

However, this will make the code clearer, as it’s easy to understand which argument corresponds to which parameter. It also allows for a more intuitive and expressive way of passing arguments, enabling students to create functions with a higher level of customization while maintaining simplicity.

Overall, keyword-only arguments in Python offer a user-friendly approach to function design, enhancing code readability and making programming more accessible for students.

Keyword-only Argument in Python- FAQs

Q1. How do you force an argument to be a keyword in Python?

Ans. The asterisk * in the function will force the behavior. Hence, any argument that is given to the parameters after the asterisk will be passed with the keyword arguments only.

Q2. What is the difference between keyword arguments and default arguments in Python?

Ans. The Keyword argument will employ the order, whereas, the default arguments will help us to work with the absence of values.

Q3. What is the use of keyword arguments __ init __ in Python?

Ans. In Python, the __init__ method is part of a class and is like a special function that gets called when we create an object from that class. It will set up the initial values for the object’s attributes. When we define the __init__ method, we always include a default parameter called ‘self,’ which represents the object being created.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment