Home  >  Article  >  Backend Development  >  Detailed explanation of examples of connectors (+, +=) in Python

Detailed explanation of examples of connectors (+, +=) in Python

高洛峰
高洛峰Original
2017-01-13 16:11:411472browse

Preface

This article introduces the connectors (+, +=) in Python to you in detail through the problems found in a piece of sample code. Without further ado, let’s take a look at the detailed introduction. Bar.

Suppose there is the following piece of code:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]
c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
 
for item in (a, b, c):
 item += [0] * (10 - len(item))
 
print a
print b
print c

The meaning of this code is that there are three lists, and it is necessary to fill 0 at the end of the list whose length is not 10, so that The length becomes 10.

The output is as follows:

[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
[5, 6, 7, 8, 9, 0, 0, 0, 0, 0]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

There is no problem here, everything is normal. However, now the requirements have changed and we need to pad the front of the list with a length other than 10 with zeros.

Then, we try to make the following changes:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8, 9]
c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
 
for item in (a, b, c):
 item = [0] * (10 - len(item)) + item
 
print a
print b
print c

Look at the output directly:

[1, 2, 3, 4]
[5, 6, 7, 8, 9]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

The result is not That's what we imagined. If you don't see the problem, keep reading. Of course, if you have already seen the clues, there is no need to waste time here.

According to our inherent thinking, the above method is feasible, such as the following example:

>>> l = [1, 2, 3, 4, 5]
>>> l = [0]*5 + l
>>> l
[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]

Such an operation allows the list to get the changes we expect.

But what if we add a few more steps:

>>> l = [1, 2, 3, 4, 5]
>>> id(l)
139935500860952
>>> l = [0]*5 + l
>>> l
[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
>>> id(l)
139935500783272

At this point, have you seen the problem? As you can see from the output of the id() method, the "l" at the back is no longer the "l" at the front.

Look at the following example again:

>>> l = [1, 2, 3, 4, 5]
>>> id(l)
139935500861024
>>> l += [0]*5
>>> l
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
>>> id(l)
139935500861024

When using +=, "l" is preceded and followed by one. At this point, we should understand the fact that the example at the beginning of the article is not inexplicable, but has a reason.

Don’t worry, let’s look at the example again:

>>> t = (1, 2, 3, 4, 5)
>>> id(t)
139935501840656
>>> t += (0,)*5
>>> t
(1, 2, 3, 4, 5, 0, 0, 0, 0, 0)
>>> id(t)
139935502151336

As you can see, when we replace the list with a tuple, the result changes again.

So what if we use the + operation on tuples:

>>> t = (1, 2, 3, 4, 5)
>>> id(t)
139935501081200
>>> t = (0,)*5 + t
>>> t
(0, 0, 0, 0, 0, 1, 2, 3, 4, 5)
>>> id(t)
139935502151336

This is the same as the list result, no different.

So, let’s take a look at the string:

>>> s = "hello"
>>> id(s)
139935500909712
>>> s += "world"
>>> s
'helloworld'
>>> id(s)
139935500909664

The result is like a tuple, "s" is using += to concatenate a string Later, it was reassigned a value, and it was no longer the previous variable. Reflected in the memory, "s" has been opened up an additional storage space to store the value.

Here, the Python connectors we are going to talk about are + and +=. It should be noted that these two symbols have different meanings in Python. One is the addition operation used in mathematics, and the other is the splicing function used on sequence types. However, when used as an addition operator, it also follows the usage rules discussed in this article. Because discussing these two symbols is essentially discussing Python's immutable and mutable, that is, variable types and immutable types. For mutable types, we can modify the variable in place, which means that its storage space is readable and writable, such as a list; for immutable types, its storage space is read-only. , it cannot be modified. If you need to perform certain operations on the immutable type to get a new result, you need to create a new storage space to store the newly generated result.

From the examples listed above, we can draw the following conclusions:

For variable types:

+: represents the connection operation, the result of which will create a new object.

+=: Represents the append operation, that is, the in-place operation, which appends the content of another object to the object in place.

For immutable types: + and += both represent connection or summation operations. There is no difference between the two. The result of the operation will produce a new object.

Let’s analyze the example at the beginning of the article. Since for iteration is equivalent to assignment, for the sake of simplicity, we only analyze a, as shown below:

>>> a = [1, 2, 3, 4]
>>> t = a
>>> id(a)
139712695835400
>>> id(t)
139712695835400
>>> t += [0]*6
>>> t
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
>>> id(t)
139712695835400
>>> id(a)
139712695835400
>>> a
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
>>>
>>>
>>> a = [1, 2, 3, 4]
>>> t = a
>>> id(a)
139712695835464
>>> id(t)
139712695835464
>>> t = [0]*6 + t
>>> t
[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> id(a)
139712695835464
>>> id(t)
139712695835400

Here, t is a reference to a, which is equivalent to the item in the example at the beginning of the article. Using += to operate on t actually operates on a, and += operates in place, so when t is changed, a also changes; if + is used to operate on t, and the result is assigned to t, then At this time, t no longer points to a, but points to [0]*6 + t, so a has not been changed.

Summary

The above is the entire content of this article. What is discussed here is just a simple issue, but I have spent such a long time talking about this issue, so I want to say The thing is, if you don't fully understand these small issues, they may cause you trouble during the programming process.

For more detailed examples of connectors (+, +=) in Python, please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn