In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "are immutable objects in Python really immutable?" in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
In daily study and work, we often encounter the problem of "immutable objects", but as we spend more and more time with the language of Python, the pit we encounter becomes more and more strange. I can't help but wonder: are immutable objects really immutable?
We know that tuples are "immutable objects", and when you try to assign a tuple, it will report an error:
Note that the language of the error report is "does not support item assignment". Tuple objects do not support assignment operations.
Example 1
Let's take a look at the following:
Although an error was reported, the value of its variable has been changed.
Reason
In fact, this is mainly because the + = operation produces new variables for "immutable objects" and binds to the original variables. However, because of the "=" sign in + =, an assignment to the tuple object is not allowed, so an error is reported, but the new object has been bound to the original variable, so we can see that the value of the variable has changed.
Example 2
There is another situation like this:
There is nothing wrong here. Why? Because append does not involve assignment operations, tuples just do not allow assignments, and append and extend are not allowed, so it is too imprecise to call tuples "immutable objects"!
Deeper.
In fact, if you know that the tuple stores the address corresponding to the element (ID), it is much easier to understand. Append and extend only modify the elements of the list, while the ID of the list itself has not changed. Only when the assignment operation is performed, the ID will change, which is not allowed to happen in the tuple.
So, to avoid stepping on these holes and forget the loose expression that tuples are "immutable objects" (of course, from an address point of view, the ID of all elements within a tuple is immutable), remember that tuples are unassignable objects.
This is the end of the content of "are immutable objects in Python really immutable?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.