Examples of connectors (+ +=) in Python are explained in detail

  • 2020-05-24 05:42:36
  • OfStack

preface

This article gives a detailed introduction to the connectors (+, +=) in Python through the problems found in 1 sample code.

Suppose you have the following 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

What this code means is that you have three lists, and you need to fill in a zero at the end of a list that is not 10 in length to make it 10 in length.

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's nothing wrong with that. 1 tangent is normal. However, now that the requirement has changed, you need to populate a list of non-10 lengths with 0's.

So, let's 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

Let's look at 1 directly:


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

It didn't turn out that way. If you don't see the problem, read on. Of course, if you can see the signs, there's no need to waste time here.

According to our fixed 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]

This action allows the list to get the desired change.

But what if we added 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

So far, have you seen the problem? through id() As you can see from the output of the method, the "l" in the back is no longer the "l" in the front.

Consider the following example:


>>> 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 += is used, there is 1 before and 1 after "l". At this point, we should understand 1 fact, the example at the beginning of the article is not puzzling, but there is a reason.

Don't worry, let's look at some more examples:


>>> 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.

What about the + operation for 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 no different from a list that turns out to be 1.

Now, what about strings:


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

The result is like a tuple, "s" is reassigned after using += to concatenate 1 string, and is no longer the previous variable. Reflected in memory, "s" has been allocated an additional storage space for the value.

Here, we're talking about the Python connectors + and +=. Note that in Python the two symbols have meanings, one for addition in mathematics and one for concatenation in sequence types. However, as an addition operator, you also follow the rules of use discussed in this article. Because talking about these two symbols is essentially talking about immutable and mutable of Python, mutable types and immutable types. For a mutable type, we can modify it in place by a variable, which means that its storage space is readable and writable, such as list; For immutable types, the storage space is read-only and cannot be modified. If you need to do something to the immutable type to get a new result, you need to allocate a new portion of the storage space to hold the new result.

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

For variable types:

+ : represents a connection operation, the result of which will create a new object. += : represents the append operation, that is, the in-place operation, which appends the contents of another object to the object in situ.

For immutable types: + and +=, which both represent join or sum operations, there is no difference between the two, and the result of their operations is a new object.

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


[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]
0

Here, t is a reference to a, equivalent to item in the example at the beginning of the article. Operation on t with += is actually operation on a, while += is in-situ operation, so when t is changed, a also changes; If t is operated with +, and the result is assigned to t, then t will no longer point to a, but to [0]*6 + t, so a has not been changed.

conclusion

That's all for this article, it's just a simple question, and I've spent so much time talking about it, so what I'm trying to say is, if you don't fully understand these little questions, they can cause problems in your programming process.


Related articles: