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

Case Analysis of python variable assignment Mechanism

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

Share

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

Most people do not understand the knowledge points of this article "case analysis of python variable assignment mechanism", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python variable assignment mechanism case analysis" article.

Let's start with the conclusion:

Variable assignment is a shallow copy (you can understand the difference between a deep copy and a shallow copy). Therefore, if it is a variable type variable (such as an is a list type, afigb) assignment, modifying a will involve b; if it is an immutable type (such as int) assignment, then modifying any variable will not be passed.

1. Variable type assignment

As you can see, for variable type assignments, variables always point to the same block address.

two。 Immutable type assignment

For the assignment of immutable type variables, at first it points to the same block address, but if you modify any variable, the modified variable points to another block address and will not affect another variable.

So the question is, which one does variable assignment for custom types belong to?

3. Custom type variable assignment

As you can see, changes to properties in custom variables are also passed.

But what if you reassign the overall variable?

As you can see, if the overall variable is reassigned, the change will not be passed, and the modified variable will point to the new address.

It is easy to trample on the difference between custom variable assignments, such as the following function, which converts the list to a linked list:

Create a copy of the head node cur, assign it to it, and then let it point to the next node, and finally return the header node, but the result is unexpected. Careful analysis shows that cur is re-assigned in the for loop, cur = Node (I), so that cur no longer points to the head node, but to another block of memory address, so that head and cur go their separate ways, and it no longer matters.

The right thing to do is:

Within the for loop, the cur itself is not changed, only its property next is changed, so that cur and head always point to the same block address, resulting in return head.next (because the first node has no data, the real node starts with head.next)

The above is about "python variable assignment mechanism case analysis" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please pay attention to 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