Why Python Has No Pointers and How to Simulate Them
This article explains why native pointers do not exist in Python, describes the underlying object model, immutable versus mutable objects, the distinction between variables and names, and demonstrates several practical ways to emulate pointer behavior using mutable containers, custom classes, and the ctypes module.
Introduction
The article introduces the concept of pointers in low‑level languages, why they are confusing for beginners, and why Python deliberately abstracts away direct memory addresses.
Why Python Has No Pointers
Python’s design philosophy favors readability and safety over low‑level memory manipulation; therefore, true pointers are not part of the language, though some pointer‑like benefits are still available.
Python Objects
Everything in Python is an object consisting of a reference count, a type, and a value. Understanding this is essential for grasping how Python handles memory.
Immutable vs. Mutable Objects
Immutable types (int, float, bool, tuple, str, etc.) cannot be altered after creation, while mutable types (list, dict, set) can change in place, which is key to simulating pointer behavior.
Variables vs. Names
Python does not have variables in the C sense; it has names that bind to objects. Assignments rebind names rather than modify underlying memory slots.
C Variables Comparison
A C variable directly occupies a memory slot, while a Python name is merely a reference to a PyObject, making the binding process different.
Pre‑implemented Objects
CPython pre‑creates small integers (‑5 to 256) and short ASCII strings to improve performance; these objects are shared across the interpreter.
Simulating Pointers in Python
1. Use mutable containers (e.g., a list with a single element) to hold a value that can be modified in place. 2. Use dictionaries to store mutable state accessible by key. 3. Define custom classes that encapsulate mutable data and expose methods for modification. 4. Employ the ctypes module to create real C‑style pointers when interfacing with external C libraries.
Using ctypes for Real Pointers
By compiling C code into a shared library and loading it with ctypes.CDLL , you can define function signatures, pass pointers via byref() , and manipulate C memory directly from Python.
Conclusion
The article summarizes that while Python abstracts away true pointers, developers can still achieve pointer‑like semantics through mutable objects, custom classes, and the ctypes module, preserving Python’s safety guarantees.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.