Fundamentals 4 min read

Understanding Python Parameter Passing Mechanisms and Argument Types

Python uses a pass‑by‑object‑reference model, meaning functions receive references to objects; this article explains how mutable and immutable objects behave under this model, describes positional, keyword, default, *args and **kwargs argument types, and provides code examples illustrating each case.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding Python Parameter Passing Mechanisms and Argument Types

Parameter Passing Mechanism

Python uses a “pass‑by‑object‑reference” mechanism. When you pass an object to a function you are passing a reference to that object, not a copy. Consequently, modifications to mutable objects (e.g., lists, dictionaries) inside the function affect the original object, while immutable objects (e.g., integers, strings, tuples) cannot be altered and therefore do not affect the original.

Argument Types

Positional Arguments – the most common way, where arguments are supplied in the order defined.

def greet(name, message):
    print(f"Hello {name}, {message}")

greet("Alice", "Good morning!")

Keyword Arguments – specify argument names, allowing any order.

def greet(name, message):
    print(f"Hello {name}, {message}")

greet(message="Good morning!", name="Alice")

Default Arguments – provide default values that are used when no corresponding argument is supplied.

def greet(name, message="Good day!"):
    print(f"Hello {name}, {message}")

greet("Bob")  # uses default message

greet("Charlie", "How are you?")  # overrides default

*args (Variable‑length Positional Arguments) – collects any number of extra positional arguments into a tuple.

def sum_numbers(*args):
    return sum(args)

print(sum_numbers(1, 2, 3))  # outputs 6

**kwargs (Variable‑length Keyword Arguments) – collects any number of extra keyword arguments into a dictionary.

def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_info(name="Alice", age=30)

Mutable vs. Immutable Objects

For immutable objects (e.g., integers, strings, tuples), any change inside a function creates a new object, leaving the original unchanged.

def modify_string(s):
    s += " world"
    print(s)  # prints "hello world"

original = "hello"
modify_string(original)
print(original)  # prints "hello"

For mutable objects (e.g., lists, dictionaries), changes inside the function affect the original object because both references point to the same memory.

def append_to_list(lst):
    lst.append(4)

my_list = [1, 2, 3]
append_to_list(my_list)
print(my_list)  # prints [1, 2, 3, 4]

Understanding these concepts enables you to control how data is passed to functions and choose the most appropriate argument passing strategy for simple calls or complex data‑processing logic.

PythonImmutable Objects*argsfunction-arguments**kwargsmutable-objectspass-by-reference
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.