Contents

Python Interactive Guide - Step 3 Functions (4) - Parameter Assignment (Pass by Assignment)

Series - Python Interactive Guide
Info
  • This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
  • The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
  • You can run and experiment with every code example.
    Feel free to try things out - reloading the page will reset everything.

This is a continuation of “Step 3 Functions.”

In Python, when you pass arguments to a function, they are assigned to parameter variables before processing.
This mechanism is called “pass by assignment” or “pass by object reference”.

While other programming languages use concepts like “pass by value” or “pass by reference,” Python’s approach is slightly different.

Let’s quickly review the concept of “assignment” that we covered in 1.4. Variables and Assignment:

What is 'Assignment'
  • In Python, “assignment” means “attaching a label (variable) to a value”.
    • When you assign a variable’s value to another variable, the value doesn’t move; both variables simply refer to the same value.
  • “Reassignment” of a variable means “moving the label to a different value”.
    • “Reassignment” doesn’t modify the original value itself; it changes what the variable points to.

Let’s examine the following code:

As you can see, reassigning the parameter variable inside the function doesn’t affect the original argument value.
(Only a “reassignment” occurs, changing what the local variable lst refers to)

Now consider this code:

In this case, the global variable prices changes after the function call.
This happens because when calling the append_item function:

  1. The local variable lst is assigned the value of the global variable prices (lst and prices refer to the same object)
  2. The object that lst refers to is modified using the append method

As a result, the value of prices is updated after the function call.

📚 Exercise

Try to predict the behavior of the following program before running it:

Sample Answer
📚 Exercise 2

Try to predict the behavior of the following program before running it:

Sample Answer
Summary of Parameter Assignment (Pass by Assignment)
  1. In Python, arguments are assigned to parameters when passed to a function (pass by assignment)
  2. Reassigning a parameter variable inside a function doesn’t change the original value
  3. Operations that modify elements of a parameter (like the append method) will change the original object

Based on the above, here are key points to remember when passing arguments to functions:

Things to Remember When Passing Arguments to Functions
  • Immutable arguments (e.g., integers, strings) won’t change when passed to a function
    • If you want to change their value, receive the changed value as a return value and reassign it to the original variable
  • Mutable arguments (e.g., lists, dictionaries) can have their contents modified inside a function, affecting the original object
    • If you want to avoid changes, create a copy (or a deep copy if needed) at the beginning of the function before processing
📚 Exercise

Create a function add_task that takes a list and a string as input and returns a list with the string appended to the end of the list.
However, make sure the original list is not modified.

(Example: add_task(["Go shopping", "Send email"], "Read a book") => ["Go shopping", "Send email", "Read a book"])

Sample Answer

Related Content