-
What is Pointer
-
Pointer In C
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.
-
Pointer in Python
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.
-
前情回顾
-
Objects
Everything in Python is an object.
Each object in Python consists of three parts:
-
Reference Count
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(). -
Type
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.
-
Value
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 object
- is checks whether two
-
理解pointer in Python
最新推荐文章于 2024-12-18 21:34:33 发布