Python’s Argument Passing Mechanism

Python’s Argument Passing Mechanism: A Casual Guide for Beginners

Today we’re going to talk about one of the most fundamental concepts in programming with Python argument passing. But before we dive into it, let me ask you something. Have you ever wondered why some functions work differently when called with different arguments? Or maybe you’ve seen code that looks like this:

# This script defines a function called "my_function" with two parameters, "arg1" and "arg2".
# The "arg2" parameter has a default value of 5, meaning it is optional and will default to 5 if no value is passed in when the function is called.

def my_function(arg1, arg2=5):
    # This line prints a string and the value of the "arg1" parameter.
    print("The value of arg1 is:", arg1)
    # This line prints a string and the default value of the "arg2" parameter.
    print("The default value for arg2 is:", arg2)

And wondered what the ***** “arg2” means?

First arguments are values that you pass to a function when calling it. They allow us to customize how the function behaves based on our needs. For example:

# This script defines a function called "add_numbers" that takes in two arguments, x and y, and returns the sum of the two numbers.
def add_numbers(x, y): # Defining the function with two parameters, x and y
    return x + y # Returning the sum of x and y

result = add_numbers(2, 3) # Calling the function with arguments '2' and '3' and assigning the result to the variable "result"
print("The result is:", result) # Printing the result with a string "The result is:" before it

In this example, we have a simple function called “add_numbers” that takes two arguments x and y. When we call it using the syntax `add_numbers(2, 3)`, Python assigns the values ‘2’ and ‘3’ to variables x and y respectively inside the function body.

But what if you don’t want to pass a value for an argument? That’s where default arguments come in handy! Default arguments are optional parameters that have a predefined value assigned to them when no other value is provided during function call. Let’s see how we can modify our previous example:

# Defining a function named 'add_numbers' with two parameters 'x' and 'y'
def add_numbers(x, y=0): # Adding 'y=0' as default argument for 'y'
    # Returning the sum of 'x' and 'y'
    return x + y

# Calling the function with only one argument (for 'x') and using default value of 0 for 'y'
result = add_numbers(2)
# Printing the result
print("The result is:", result)

In this modified example, we have added a default argument to our “add_numbers” function. Now if you call it without passing any value for the second argument (‘y’), Python will automatically assign the predefined value of 0 to ‘y’. Pretty cool, right?

But what happens when you pass both arguments one with and one without a default value? Let’s find out!

# Defining a function called "add_numbers" with two parameters, 'x' and 'y'
def add_numbers(x, y=0): # Adding 'y=0' as default argument for 'y'
    # The function will return the sum of 'x' and 'y'
    return x + y

# Calling the "add_numbers" function with two arguments, 2 for 'x' and 3 for 'y'
result = add_numbers(2, 3) # Calling the function with both arguments (for 'x' and 'y')
# Printing the result of the function call
print("The result is:", result)

In this example, we have called our “add_numbers” function with two arguments ‘2’ for ‘x’ and ‘3’ for ‘y’. Since a value was provided for both arguments, Python will use the values passed by us instead of using their default values.

But what if you want to pass multiple keyword arguments (arguments that have names) to your functions? That’s where **kwargs come in handy! **kwargs is a special syntax used to accept an arbitrary number of keyword arguments as a dictionary. Let’s see how we can modify our previous example:

# Defining a function named 'my_function' that accepts multiple keyword arguments using the special syntax '**kwargs'
def my_function(**kwargs):
    # Printing the value of the keyword argument 'arg1' passed to the function
    print("The value of arg1 is:", kwargs['arg1'])
    # Checking if the keyword argument 'arg2' is present in the dictionary 'kwargs'
    if 'arg2' in kwargs:
        # If present, printing the value of 'arg2'
        print("The value of arg2 is:", kwargs['arg2'])

# Calling the function with only one keyword argument 'arg1'
my_function(arg1=5)
# Calling the function with both keyword arguments 'arg1' and 'arg2'
my_function(arg1=5, arg2=7)

In this modified example, we have added **kwargs to our “my_function” function. Now if you call it without passing any value for a keyword argument (‘arg2’), Python will not assign anything to that variable inside the function body. But if you pass both arguments one with and one without a default value (like in our previous example), Python will use the values passed by us instead of using their default values.

SICORPS