Search⌘ K
AI Features

References

Explore how Python assigns and manages references for simple values and complex objects like lists in classes and functions. Understand the impact of mutable and immutable types on data changes, helping you avoid common pitfalls in object-oriented programming with Python.

We'll cover the following...

Simple values and complex objects

In a technical sense, every value in Python is an object. In practice, there is a distinction between simple values and more complex objects. Numbers and Booleans can be considered simple values.

Let’s say, we have two values: a and b. If the value in variable a is simple, and you execute the assignment b = a, the value is copied from a into b. You can subsequently change the value in either a or b without affecting the value in the other variable.

However, consider the following scenario:

Python 3.5
a = [1, 2, 3]
b = a
b[1] = 99
print(a) # prints [1, 99, 3]

In this example, a is a list, and lists are a kind of object. This list ...