Pointers allow you to create great efficiency in parts of your code. They also cause confusion for beginners and can lead to various memory management bugs.
Essentially, they are variables that hold the memory address of another variable for memory management.
Whenever we create a variable or an object in a programming language, it’s stored in a particular CPU address. Whenever we output the data, it pulls from that address.
Pointers in Python don’t really exist.
For the cases where you need to mimic pointer behavior, you can simulate pointers in Python without the memory-management nightmare.
Python tends to try to abstract away implementation details like memory addresses from its users. Python often focuses on usability instead of speed. As a result, pointers in Python don’t really make sense.
Everything in Python is an object.
Each object in Python consists of three parts:
It deals with the memory in the CPU. It represents the number of Python variables referring to a memory location.
You can get the exact value by sys.getrefcount(object) which return the reference count of the object. From its documents, the count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount().
It refers to the kind of object like int, float, string, etc…
The type is used at the CPython layer to ensure type safety during runtime.
It’s the actual value of an object stored in the memory.
Objects are of two types Immutable(int, float, double, str, bool, complex, tuple) and Mutable(list, set, dict).
We can test whether an object is immutable or mutable by using id() and is :
id() returns the memory address of the objectis checks whether two objects have the same memory address or notWe can’t change the immutable objects once we create them.
When we assign a to b, Python doesn’t create a new object. It merely made reference of b to a value. It saves memory.
We can change the mutable objects even after creation, even after performing some operations on the list, the address doesn’t change. Because the list is a mutable object.
Python doesn’t create new objects when we modify a mutable object.
C variables memory management is entirely different from Python.
// C systax not Python int a = 918Execution steps of the above statement.
Allocated encough memory for the intergerAssigns a value 918 to the memory locationMakes a refer to the value of 918 , which indicate that a points to the value 918Now a has a memory location, when we update the value of a :
a = 849The address location of a doesn’t change.
The variable is not just a name for the value, it’s a memory location itself.
So, we are overwriting the value of a memory location directly. It’s completely different from Python.
If we want to assign a to another variable, then C creates a new memory location, unlike Python:
int b = awhich b has a different memory location with a . C creates a new memory location for b and copies the values of a and assigns them to b.
C creates a new memory location for every variable we create.
Generally, variables in the Python are called names.
# code in Python a = 918The above code will undergo the following steps during execution.
Creates a new PyObjectSets data type as an integer for PyObjectSets the value 918 to the PyObjectCreates a name as we define (a)Points a to the PyObjectIncrements the Refer Count of PyObject from 0 to 1a reference memory location illustration in Python:
PyObjectTypeintValue918Reference Count1While in C:
aLocation0x661Value918a in Python is not a memory location as it was in C variables.
When we assign a new value to a:
a = 98The above statement undergoes the following steps during exeution.
Creates a new PyObjectSets data type as an integer for PyObjectSets the value 98 to the PyObjectPoints a to the new PyObjectIncrements the Refer Count of the PyObject by 1Decrements the Refer Count of the old PyObject by 1In memory a refers to the new PyObject. So, the old PyObject refer count will be 0.
if we assign a to a new variable b :
b = aPython doesn’t create a new PyObject for the variable b. Python makes the reference count of a’s PyObject to two. Those two variables are a and b.
a and b refers to the same PyQbject:
PyObjectTypeintValue98Reference Count2To see whether a and b referred to the same memory location or not, run the following code:
>>> a is b TrueIf we modify the value of b ,Python creates a new PyObject for b because integers are immutable in Python.
Using ctypes module, we can create real pointers in Python.
The pointer we implemented with ctypes are real pointers.
You can simulate Pointers in Python, or create real C-style pointers in Python.
The real reason you would use ctypes is if you needed to make a function call to a C library that requires a pointer.
《理解ctypes模块in Python》
Python variables are fundamentlly different than variables in C or C++.
In fact, Python doesn’t even have variabels.
Python have names, not variables.
This might seem pedantic, and for the most part, it is. Most of the time, it’s perfectly acceptable to think about Python names as variables, but understanding the difference is important. This is especially true when you’re nacigating the tricky subject of pointers in Python.
From above we know that, every variable has a memory location, and if you assign a new value to the variable, the variable didn’t change its memory location, just change the value of the memory location. Which means variable in C is the memory location, not just a name for it.
Python does not have variables.
The PyObject is not the same as Python’s object. It’s specific to CPython and represents the base structure for all Python objects.
In Python, instead of x(Python name) owning the block of memory where the value resides, the newly created Python object owns the memory where the value lives.
The Python name x doesn’t directly own any memory address in the way the C variable owned a static slot in memory.
When creating a new name y in Python which y = x, no new Python object has been created, just a new name that points to the same object which increas its refer count by 1.
To recap, in Python, you don’t assign variables. Instead, you bind names to references.
>>> x = 1000 >>> y = 499 + 501 >>> x is y FalseHere are the steps that occur when this code is executed:
Create Python Object (1000)Assign the name x to that objectCreate Python Object (499)Create Python Object (501)Add these two objects togetherCreate a new Python object (1000)Assign the name y to that objectThe above steps occur only when this code is executed inside a REPL.
Some smart code refered as interned objects. Which prevents memory allocation calls for consistently used objects. CPython 3.7 interns the following:
Integer numbers between -5 and 256Strings that contain ASCII letters, digits, or underscores only