Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Python Doesn't Have Pointers

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

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

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

00:22You 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:36The code becomes harder to read because you have to figure out if the variableyou’re dealing with contains the actual value or just a memory address to one.

00:47Even 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 likememory management. This makes the idea of pointers kind of useless.

01:09When 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’reno longer accessible, something that would normally require the use of pointerswhen dealing with large objects.

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

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

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

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

02:18This is a trade-off you have to consider when starting a new programmingproject. 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 isoften a better choice. As you’ll learn later on,we can mix the two and get the best of both worlds.

02:45Before I can show you how you can simulate pointer behavior in Python,we first need to talk aboutmutable versusimmutable objects.

Become a Member to join the conversation.

Course Contents

Overview
33%

[8]ページ先頭

©2009-2026 Movatter.jp