In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is the example analysis cited by Python and C++. The editor thinks it is quite practical, so I hope you can get something after reading this article.
I encountered this problem when writing code to build a binary tree in Python. The reason is to confuse the "reference" of Python with the "reference" of C++. There are "reference" nouns in both languages, although they are similar most of the time, but there are differences. Note that this is just my own personal understanding. Let's understand it this way before we find a better understanding.
For example, these are my two pieces of code to create a binary tree based on a preorder sequence:
# correct pos = 0def create (seq): global pos ch = seq [pos] pos + = 1 if ch = ='#': return None else: temp = TreeNode (ch) temp.left = create (seq) temp.right = create (seq) return temp seq = "abd###ce###" root = create (seq)
And this:
# error def create (root): global pos if (seq [pos] = ='#'): root = None pos + = 1 return root = TreeNode (seq [pos]) pos + = 1 root.left = None root.right = None create (root.left) create (root.right)
It seems that there is only a difference between whether there is a return value or not, but in fact, the second code is wrong. In fact, from the point of view of the second code, it can be written like this in C++:
Void create (tree & t) {char ch= str [pos++]; if (ch=='#') {t = NULL; return;} t = new node; t-> val = ch; create (t-> left); create (t-> right);}
This is the difference between references in Python and those in C++.
I understand it this way: the use of memory in Python is more immutable. For the object that a reference points to, if I want to modify the value, I will first find a new piece of memory, assign the value, and then point the reference to the new memory block. This is to change the location that the reference points to, but in C++, it changes directly on the memory block pointed to by the reference, for example:
Python:a = 1print (id (a)) a = a+1print (id (a)) # output 140708803374848140708803374880
In C++:
Int t = 1% d\ n ", & a, a); a + = 1% d\ n", & a, a); # output 7208712 17208712 2
So, like the second piece of code above, I pass in a reference to the root of the tree I created, but when the structure of the tree changes, it actually changes the location of the storage, so the root reference created outside the original function actually points to the original empty memory block. It's like being stolen and still waiting. But the reference of C++ stubbornly recognizes only that piece of memory, so it can be changed directly.
There is also some knowledge about the relationship between references and objects in Python, such as memory, such as:
A = 1b = 1print (id (a)) print (id (b)) # output 140708803374848140708803374848
This is because in order to save memory, some short strings and numbers are cached in Python, which saves the overhead of establishing and destroying such objects.
Others are about mutable data objects (mutable) and immutable objects (immutable), which I have recorded before. There are always some languages that seem simple, but in fact they have to be well understood.
The above is the example analysis cited by Python and C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
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.