Python pointers, it is necessary to understand it

thumbnail

[Introduction]: This article mainly talks about pointers in Python, and Python automatically manages memory. The developer does not need to manually allocate memory for the object, nor release the memory after using the object. But understanding Python’s memory management mechanism will help developers write better code. This article will introduce the concept of pointers and explain deep copying.

Introduction

Compared with static languages ​​such as C and C++, Python manages memory automatically. It manages memory by means of “reference counting”, that is, how many references an object has internally will be recorded in Python. If the reference count of an object is greater than 0, The object will always be stored in memory; when the reference count of the object is 0, it will be reclaimed by the garbage collection mechanism. This is also the convenience of Python. Developers do not need to consider allocating memory for objects in advance, and they do not need to manually release memory after use.

But it is also necessary to understand how Python manages memory internally. Have you considered the difference between is and ==? Or why use deep copy? Have you ever considered how Python works with objects? This article may give you some inspiration.

What are pointers and where do they exist?

First, we need to understand the concept of namespaces. A namespace in Python refers to a list of all variables, keywords and functions in a scope to avoid naming conflicts. Different namespaces can contain the same variable name. For example, in each namespace there are built-in functions (like print and str), keywords (like None and True).

When you create a new variable, the variable name is added to the namespace of its own scope. For example, the following code will add the variable name my_string to the global namespace:

my_string = “Hello World!”

In this article we do not consider scope issues, all cases are based on the global namespace.

A pointer is a variable name—that is, an entry into the Python namespace—that corresponds to an object in Python memory. In the above example, the pointer is my_string and the object in memory is the “Hello World!” string. By using pointers in namespaces, we can access and manipulate objects in memory. Just as a person may have multiple names, multiple pointers may point to the same object.

Note: The “pointers” mentioned in this article are not pointers in C or C++ (more like references in C++). Developers using C can take a look at this article [1].

Next, we first create a list named my_list: my_list = [‘string’, 42] .

The variable name my_list points to the list object. The list object has 2 pointers, each pointing to the 2 elements in it. It can be seen that when a list is created, if there are elements in it, the list will also contain pointers to those elements. Therefore, many of the examples in this article will use lists.

Why use deep copy?

Beginners often don’t understand what is pointer overlap, in short, two pointers point to the same object in memory. Next, we first create a list a:

>>> a = [ “string”, 42]

>>> a

[ “string”, 42]

Then, copy a, get b, and modify the first element of b:

>>> b = a

>>> b[0] = “some words”

>>> b

[ “some words”, 42]

As you can see, the modification was successful, but the result of a also changed:

>>> a

[ “some words”, 42]

a changes because b = a doesn’t create a new list, it just creates a new pointer b that points to the same list as a.

What if we want to modify b without creating a new list? We can use the copy method:

>>> c = a.copy

>>> c[0] = “hello!”

>>> c

[ “hello!”, 42]

>>> a

[ “some words”, 42]

This method will create a new list with new pointers, but these pointers point to the same elements as the original list, as shown below:

Let’s look at the case where the list elements are also objects:

>>> a = [[ “alex”, “beth”]]

>>> a

[[ “alex”, “beth”]]

Next, use the copy method to copy the list a, and add elements to the first element of b [“alex”, “beth”]:

>>> b = a.copy

>>> b[0].append( “charlie”)

>>> b[0]

[ “alex”, “beth”, “charlie”]

It seems to succeed, but the elements of a[0] also change:

>>> a[0]

[ “alex”, “beth”, “charlie”]

The reason is that the copy method is a shallow copy. a[0] and b[0] point to the same object, as shown below:

So, how to implement deep copy? You can use the deepcopy function:

>>> from copy import deepcopy

>>> c = deepcopy(a)

>>> c[0].append( “dan”)

>>> c[0]

[ “alex”, “beth”, “charlie”, “dan”]

>>> a[0]

[ “alex”, “beth”, “charlie”]

The deepcopy function uses a recursive method to copy each object to avoid pointer aliasing. The following figure is the new object c generated by the deep copy method:

In this way, when we modify the elements in c, it will not affect the source object a.

pointers in immutable objects such as tuples

So far, our examples have been using lists, which are mutable objects. Next look at immutable object tuples. It is said to be immutable because when a is created, all its elements a[0], a[1] are fixed. If the element is immutable, like a string or an integer, it’s easier to understand. For example, in the following example, we cannot modify a[0]:

>>> a = (42, ‘beeblebrox’)

>>> a[0] = 63

Traceback (most recent call last):

File “<stdin>”, line 1, in<module>

TypeError: ‘tuple’object does not support item assignment

What happens if a[0] points to a mutable object, such as a list?

>>> a = ([1, 2, 3], “hello”)

>>> a[0].append(4)

>>> a

([1, 2, 3, 4], “hello”)

It turns out that we can modify the tuple a! This is because we are not modifying a[0] itself, but the object it points to. We rename the object pointed to by a[0], and it looks a lot clearer:

>>> my_list = [1, 2, 3]

>>> a = (my_list, “hello”)

>>> my_list.append(4)

>>> a

([1, 2, 3, 4], “hello”)

However, the append method is not the only way to add elements to a list. We can also use the += operator:

First, the operator creates the desired object. For mutable objects (like lists), it modifies the object directly; for immutable objects (like strings), it creates a new object.

Second, let the pointer of my_list point to the created object.

  • mutable object

mutable object

>>> my_list += [5, ]

>>> my_list

[1, 2, 3, 4, 5]

If calling += on a mutable object, then step 2 is rather redundant - after all, the pointer already points to the desired object. But when it’s called on an immutable object (like a string), it does need to change where the pointer points. E.g:

  • immutable object

immutable object

>>> my_string += ‘, World!’

>>> my_string

‘Hello, World!’

Suppose we use += to operate on the first element of tuple a, right? come and see:

>>> a = ([1, 2, 3, 4], “hello”)

>>> a[0] += [5, ]

Traceback (most recent call last):

File “<stdin>”, line 1, in<module>

TypeError: ‘tuple’object does not support item assignment

>>> a

([1, 2, 3, 4, 5], “hello”)

It seems that there is an error, but it seems that += is enforced.

First, the list pointed to by a[0] itself has changed, which is why the value of a[0] becomes [1, 2, 3, 4, 5]. However, since the pointer to the element in the list cannot be directly modified, an error will be reported when the pointer of a[0] points to the newly created object.

postscript

In this article we learned the concept of namespaces, pointers, and provided some examples to explain how pointers work in immutable objects. Due to space limitations, this article does not cover Python objects. If you find the content of this article helpful, please like and forward it to support it. Continue to update other contents of the Python pointer later.

References

[1]

This article: https://realpython.com/pointers-in-python/

[2]

Reference original: https://anvil.works/articles/pointers-in-my-python-1

  • EOF -

Add homepage Jun WeChat, not only Python skills +1

Homepage Jun will also share Python-related tools , resources and selected technical articles on personal WeChat every day , and occasionally share some interesting activities , job promotions and how to use technology to do spare projects

Add a WeChat, open a window

Click on the title to jump

  1. Increase knowledge! Python exception information can also be displayed like this

  2. What programming language is Cython? why you need to study

  3. 6.6K Star! Much faster data processing library than Pandas

Think this article was helpful to you? please share with more people

It is recommended to pay attention to “Python developers” to improve Python skills

Like and watching is the biggest support ❤️

Latest Programming News and Information | GeekBar

Related Posts