In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "Why Python does not support I + grammar". In the operation of actual cases, many people will encounter such a dilemma, so 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!
Normally, this line gets my attention when someone asks about the cause of + + rather than the operator in Python.
If you want to know the original reason, you must flip through the old Python mailing list or ask someone there (for example, Guido) ~ through stackoverflow
This forces me to think like the picture above. Really, do I have to ask Guido why? Okay, maybe, but before that, I should give it a try, which prompted me to write this article
In languages such as C / C + / Java, self-increment or self-subtraction of integer variables is standard, which can be divided into prefix operations (+ I and-I) and suffix operations (I + and I -), each with subtle differences and different purposes.
When users of these languages enter Python, they may wonder why it doesn't provide + or-operations.
Although the prefix + + I may appear in Python, it is not a "+ +" self-increasing operator.
In this case, it is just a superposition of two "+" (plus signs), and the "+" suffix is not supported at all! (SyntaxError: invalid syntax).
So why doesn't Python support I + incremental syntax?
First of all, Python can certainly achieve self-enhancement, that is, it can be written in the form of I + = 1 or I = I + 1, which is also common in other languages.
Although Python uses different magic methods (add () and _ _ iadd _ ()) at the bottom to complete the calculation, the apparent effect is exactly the same.
Therefore, our question can be translated into: why are the above two writing styles better than I + + and become the final choice for Python?
1. Python integers are immutable types
When we define I = 1000, different languages treat them differently.
Languages such as C (write int = 1000) will apply to memory space and bind them to the fixed name I while writing the variable value 1000.
Here, the address and type of I are fixed, while the value is variable.
Python (write I = 1000) will also apply for memory space, but it will "bind" to the number 1000, that is, the address and type of 1000 are immutable
Therefore, when we make me "self-increase" (I = I + 1), both are treated differently.
Languages such as C first find the value stored in the I address and then add 1 to it. After the operation, the new value replaces the old value
The operation of Python is to add 1 to the number I points to, then bind the result to the storage space of the new application, and then "paste" the name label to the new number.
For example, I in C is like a host with 1000 parasites, while 1000 in Python is like a host with I. So I am the same as 1000 in Python in C language.
To sum up, let's take a look at I +, and it is not difficult to find:
In languages such as C, I + + can represent an increase in the numerical properties of I, neither opening new memory space nor creating new first-class citizens.
In a language like Python, this doesn't make any sense if I + + is an operation on its name attribute. If it is understood as an operation on the digital ontology, then the situation will be very complicated.
It will generate a new first-class citizen 1001, so it needs to be assigned a memory address. If the address occupied at this time is 1000, the recycling of old objects will be involved, and the original reference relationship with 1000 will also be affected. So you can only open new memory space for 1001.
If Python supports I + +, its operation will be more complex than C + +, and its meaning is no longer "increase the number by 1" (increment), but rather "create a new number".
Python can theoretically implement I + + operations, but it must redefine the "incremental operator", which will lead to misunderstanding by people with experience in other languages.
It's best to have everyone write I + = 1 or I = I + 1 directly.
2. Python has iterable objects
Languages such as C / C + + have designed I + + primarily to facilitate the use of a three-part for loop structure.
This kind of program is related to the self-increasing process of the number itself. The increase in number is related to the execution of the program subject.
There is no such structure in Python, which provides a more elegant approach:
This reflects different ways of thinking.
It is concerned with iterative traversal within the range of values.
It doesn't care or doesn't need to add numbers artificially.
The iterable object / iterator / generator in Python provides a good iteration / traversal usage and can completely replace I +.
For example, Python can use enumerate () to traverse subscripts and specific values at the same time as the values in the example above.
To give another example, for dictionary traversal, Python provides keys (), values (), item (), and other traversal methods, which are very easy to use:
Not only do I + = 1 or I = I + 1 are rarely used in Python, but the availability of iterative objects makes it easy to manipulate a range of values, and it is rarely required to accumulate.
So, going back to our original question, these two "self-increasing" methods are not much better than I +, simply because they are general operations.
This is why there is no need to introduce new operators, and Python continues to provide basic support. The real winners are a variety of iterable objects!
Abstract
Python does not support the increment operator for the following reasons:
Because its integers are a class of constant first-class citizens, it will lead to ambiguity if it is to support incremental operations (+ +).
That's all for "Why Python doesn't support I + + grammar". Thank you for 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.