cspy

Pointers

Chuck Sailer
July 2nd, 2023

In Python, you don’t directly use pointers as in languages like C or C++. However, Python does have its own system for referencing objects that behaves somewhat similarly to pointers, and understanding this system can be helpful.

In Python, variables are references to objects. When you assign an object to a variable, you’re actually creating a reference to the object, not creating a new copy of it. This is similar to how pointers work.

Here’s an example to illustrate this:

a = [1, 2, 3]  # a is a reference to a list object
b = a          # b now refers to the same object as a

b.append(4)    # modifying the object through b...

print(a)       # ... also changes what you see when you access it through a!

Output:

[1, 2, 3, 4]

In this code, a and b are both references to the same list object. When you modify the list through b, you can see the changes when you access the list through a because they’re both pointing to the same underlying object. This is similar to how pointers are used to reference and manipulate memory in C or C++.

However, Python handles all of this behind the scenes, so you don’t have to manually manage memory addresses or worry about dereferencing pointers or other such low-level details. This makes the language easier to use but can sometimes cause confusion if you’re not aware of how Python’s object referencing system works, particularly when working with mutable vs. immutable objects.

In Python, understanding how references work is important for understanding how arguments are passed to functions (i.e., everything is passed by object reference), how to correctly copy complex objects (like nested lists), and other such tasks. But unlike in C or C++, you don’t work directly with memory addresses in Python, so there’s no direct equivalent to pointers in the language.