Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python Doesn't Have Pointers

Why doesn’t Python have pointers? In this lesson, you’ll learn how pointers seem to go against the Zen of Python. Pointers encourage implicit changes rather than explicit ones, and they are often complex rather than simple.

00:00 Python does not actually support the idea of pointers. You can’t use the reference or dereference operator to obtain memory addresses or follow them. No one really knows why Python doesn’t support these.

00:15 But if I had to guess, I’d say it’s because pointers go against the Zen of Python.

00:22 You see, pointers probably seemed fairly elegant in the last two videos, but the truth is they’re often very messy to work with. Pointers encourage implicit changes rather than explicit ones.

00:36 The code becomes harder to read because you have to figure out if the variable you’re dealing with contains the actual value or just a memory address to one.

00:47 Even worse, they beg for you to shoot yourself in the foot or do something dangerous, like read from a section of memory you weren’t supposed to. Like other higher-level languages, Python tends to abstract away from these low-level implementation details like memory management. This makes the idea of pointers kind of useless.

01:09 When you pass a variable to a function, you don’t have to worry about any real memory management. Python will make sure to free up the memory that your variables use when they’re no longer accessible, something that would normally require the use of pointers when dealing with large objects.

01:27 This automatic process is called garbage collection, and it happens in the background without you having to do anything at all.

01:36 All of this fancy, behind-the-scenes work is why Python is a slower language than many of its low-level siblings, like C. Writing a program in Python is often faster and easier since all you

01:49 have to worry about is the scope of your variables. You don’t have to worry about how these variables are referenced under the hood using pointers. This comes at a performance cost though.

02:01 Even though a C program might take longer to write and is much easier to get wrong, when that code does work, it’s going to be a lot faster than its Python equivalent. Without background processes like garbage collection, C absolutely flies.

02:18 This is a trade-off you have to consider when starting a new programming project. If you’re programming something that deals a lot with hardware, like memory, such as a graphics driver or a game engine, you’ll want to use C. For anything that isn’t so performance-critical, Python is often a better choice. As you’ll learn later on, we can mix the two and get the best of both worlds.

02:45 Before I can show you how you can simulate pointer behavior in Python, we first need to talk about mutable versus immutable objects.

Become a Member to join the conversation.