Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the considerations for Python enhanced assignment and shared references

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about Python enhanced assignment and shared citation points for attention. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Python enhanced assignment and shared reference considerations

The enhanced assignment in Python is borrowed from the C language, so most of these formats are the same as C, which is the shorthand of expressions, that is, the combination of binary expressions and assignment statements, such as a + = b and a = a + b, for example, there are also the following enhanced assignment statements.

That is, enhanced assignment statements apply to any type that supports implicit binary expressions, such as the polymorphism of "+": the addition of numbers and the merging of strings.

Digital addition and subtraction

A = 1a = a + 1print (str (a)) a + = 1print (str (a))

Example result:

twenty-three

String merging

S = 'Python.'print S = S +' like 'print (S) S + =' Python.'print (S)

Example result:

I likeI like Python.

Advantages

Brevity

Reduce the execution of a, and the execution speed is faster

For mutable objects, enhanced assignment automatically chooses to perform the modification operation in place, rather than a slower copy. This leads to the problem of shared references that we may involve in mutable objects.

Shared reference

What do we do when we want to expand the list, such as adding a set of elements to the end?

L = [1,2,3] # traditional "+" method L = L + [4,5] print (L) # using list method extendL.extend ([6,7]) print (L)

Sample result

[1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6, 7]

In the first example, the "+" method, that is, using the merge method, requires the creation of a new object to copy the L on the left to the new list, and then [4, 5] to the new list. The second kind of extend is to add [4,5] directly to the end of the memory space list L, that is, the speed will be faster, and the enhanced assignment is automatically adopted, that is, L.extend ([6,7]) and L + = [6,7] are equivalent and the best choice. Although this kind of merge is fast, the shared reference to mutable objects becomes trickier.

L1 = [1,2,3] L2 = L1L2 = L2 + [4,5] print (L2) print (L1) print ('-'* 21) L1 = [1,2,3] L2 = L1L2 + = [4,5] print (L2) print (L1)

Example result:

[1,2,3,4,5] [1,2,3]-[1,2,3,4,5]

We can see from the example that if multiple variables are assigned to the same mutable object, then when breaking the shared reference structure, the mutable object should be copied and manipulated.

The above is the Python enhanced assignment and shared reference notes shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report