In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to realize the assignment logic in Python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Chapter I introduction of examples
Let's start with a set of seemingly contradictory code:
# Code 1 > > a = 3 > > b = a > b = 5 > A3
It seems easy to understand. In the second step, a simply copies the value to b, and then b is updated to 5. An and b are two independent variables, so the value of an is certainly not affected. C language mode.
Is that true?
Let's look at another piece of code:
# Code 2 > a = [1,2,3] > > b = a > > b [0] = 1024 > a [1024, 2,3]
In the second step, a simply copies the list to b, then updates the value of b [0], and finally outputs a, but an is also changed.
According to the logic of code 1 (that is, variables are independent), an in code 2 should not be affected.
Why is there such a difference?
Chapter two Python's counterintuition
Let's not explain the "seemingly contradictory" problem above.
Let's first take a look at another simple set of Python code that looks like in memory:
# Code 3b = 3b = b + 5
The schematic diagram of its operation in memory is as follows:
However, in the literal sense of the code, "assign 3 to b, add b to 5 and then assign it to b."
That is, think of the code as this:
B ← 3b ← b + 5
So the following in-memory operation diagram may be more intuitive:
That is, the value of b + 5 is written back to b. A typical C program is like this. Assign an int-type memory unit to the variable b, and then store the integer 3 in that memory unit. B represents the block of memory space, no longer move, you can update the value of b, but the address of b in memory will no longer change. So when we say b = b + 5, it is equal to b ← b + 5, and we still put it in b after adding the value of b by 5. The variable b is tightly bound to the memory space it is located in, and the human form is one.
Looking at the memory diagram in Python above, b + 5 gets a new value, and then makes b point to the new value. In other words, it does things like this:
B → 3b → b + 5
Shilling b points to 3, and then b points to the new value b + 5.
The C program updates the value stored in the memory unit, while the Python updates the direction of the variable.
Variables in the C program hold a value, while variables in Python point to a value.
If the C program manipulates the data indirectly by manipulating the memory address (each variable always corresponds to a memory address, so manipulating the variable is to manipulate the memory address), the data is in a passive position, then Python manipulates the data directly, the data is in an active position, and the variable exists only as a reference relationship, and no longer has the storage function.
In Python, each piece of data takes up a memory space, and the new data such as b + 5 also takes up a whole new memory space.
This kind of operation of Python makes the data become the main body, and the data and the data interact directly.
Data is called Object in Python.
This sentence is not very strict. But it is true in this simple example.
An integer 3 is an int object, a 'hello' is a string object, and a [1, 2, 3] is a list object.
Python regards all data as "objects". It allocates a memory space for each object. After an object is created, its id no longer changes.
Id is the abbreviation of identity. It means "identity; identity".
In Python, you can use id () to get the id of an object, which can be seen as the address of the object in memory.
After an object is created, it cannot be destroyed directly. So, in the previous example, the variable b first points to object 3, and then continues to execute b + 5 and b + 5 produces a new object 8, and since object 3 cannot be destroyed, make b point to the new object 8 instead of overwriting object 3 with object 8. After the code execution is complete, there is still object 3 and object 8 in memory, and the variable b points to object 8.
If no variable points to object 3 (that is, it cannot be referenced), Python uses a garbage collection algorithm to decide whether or not to recycle it (this is automatic and does not require the programmer to worry about it).
An old object cannot be overwritten, and the new data generated as a result of the old object interaction will be placed in the new object. In other words, each object is an independent individual, and each object has its own "sovereignty". Therefore, the interaction between two objects can produce a new object without affecting the original object. In large programs, the interactions between objects are complex, and this independence makes these interactions secure enough.
The C program assigns a fixed memory address to each variable, which ensures the independence of C variables.
C language is the interaction between variables (that is, memory addresses), and Python is the interaction between objects (data). These are two different ways of interacting.
So what are the benefits of direct interaction between data like Python?
Unfortunately, this is not the content of this article, which is the core of object-oriented design. This paper only makes some comparisons between the interaction mode of Python and that of C language in order to distinguish the logical and physical differences between them.
It is believed that this logic will help you write Python programs better and help you understand object-oriented programming more deeply in the future.
This chapter adds:
The assignment of Python changes the pointing relationship of the variable, so it is easier for Python to read an assignment expression from front to back.
/ / C language b ← bounded 5 / / assign the value of bread5 to b # Pythonb → baked 5 # make b point to bread5 Chapter 3 answer the questions in Chapter 1
Take a look at code 1:
# Code 1 > > a = 3 > > b = a > b = 5 > A3
All data in Python is objects, and numeric types are no exception. 3 is an object of type int, and 5 is an object of type int.
In the first line, a points to object 3.
In the second line, make b also point to object 3 that a points to.
The third line, because the object cannot be overwritten (destroyed), makes b point to the new object 5, and only a points to object 3.
The fourth line, output a, gets 3.
Diagram of the operation in memory (Python):
This is completely different from the explanation in the first chapter, which is explained in C language:
These are two completely different mechanisms.
In Python, b first points to object 3, but because of the independence between objects, one object cannot overwrite another object, so b points to object 5 instead of replacing object 3 with object 5 in memory.
Let's look at code 2:
# Code 2 > a = [1,2,3] > > b = a > > b [0] = 1024 > a [1024, 2,3]
The first line, make a point to a list [1, 2, 3]
In the second line, make b also point to the list pointed to by a
In the third line, make b [0] = 1024 1024 although it is an object, it does not attempt to overwrite the object pointed to by b, but modifies the first element of the object. Modify, not overwrite, so it can be manipulated by the original object instead of having b point to the modified object.
The list pointed to by an output on the fourth line has also changed.
Diagram of the operation in memory (Python):
An object whose value can be modified is called an immutable object. Common lists and dictionaries are mutable objects.
Because its value can be modified, if there are multiple variables pointing to the list:
A = [1,2,3] b = ac = ad = a.
Then use b, c, d,. Can access the object and modify its contents This feature is often used to pass parameters to a function, and if the parameter of the function is a mutable object, then the function can modify the contents of the "argument":
> a = [1,2,3] > > def change (t): t [0] = 1024 > change (a) > > a [1024, 2,3] >
When you call the function change, make t also point to the list that a points to, and then use t to change the first element in the list, changing it, not overwriting it, so the change to the object that t points to also changes the object that "argument" a points to. On the other hand, the C language cannot change the content of the argument because the argument to the parameter is passed by value (although it can be achieved with the help of a pointer, but only in general).
But in areas outside the function, we should try to avoid using it this way, which can easily lead to errors (of course, it can be useful sometimes, depending on your program). For example, in multi-person collaborative programming, if An accidentally modifies a variable object, then B, C, D and other people who use the object will be affected.
For immutable objects (immutable object), that is, objects whose values cannot be changed, passing a function does not affect the value of the argument:
> a = 5 > def add (n): n = n + 2 > add (a) > > a5
When calling the function add, make n also point to the object 5 that a points to, and then add the object 5 pointed to by n = n + 2 to object 2 to get a new object 7. Because one object cannot overwrite another object, n points to the new object 7 without changing the original object. Therefore, the value of a has not changed. Although it is consistent with the result of the C program, it is completely different from the mechanism of the C program. The reason why the C program does not change an is that only the value transfer occurs when the function is called, that is, only the value of an is copied to n.
Don't confuse these two kinds of assignment logic, they have completely different physical implementations.
Different logic of thinking will lead to different logic of writing. Although the results of these two kinds of logic are consistent in many cases, they cannot be simply assumed to be the same. Otherwise, if you make mistakes in some small details, it will be difficult to understand. You can only memorize something by rote and memorize something as a special case of Python. Although "only your hands are familiar" can also make you go far, but when you think correctly, you can not only go further, but also go easier.
For example, when your mind is clear, the answers to the following questions will naturally come out:
Why is the return value of the method of the list mostly None?
Why is the return value of a string method mostly a new object?
Why is there no self-increment / self-subtraction operator in Python? It points to a self-increment of the original memory address.
Why can't the value of "argument" be modified by the function after some variable objects are passed into the function?
(for example, change the body of the above change function to t = t [1:]. After calling the function, the object that a points to does not change. ) replication of object
……
These are not relevant to the topic of this article, so the answers are no longer listed.
Interesting addition:
1. Number is a natural immutable object (immutable object).
For n = n + 2, one might say, why can't you think of it as a modification like a list, where n still points to the original object, so that after add (a), a becomes 7, but why not?
Because each number is a single object, and the object cannot overwrite the object. So the sentence actually means: the object a points to plus object 2 produces a new object, and then makes a point to the new object a + 2.
Therefore, there is no modification of the number type, it is a natural immutable object.
two。 Why are there no self-increment (+) and self-subtraction (- -) operators in Python?
Self-increasing or self-subtracting operators are commonly used in C language and are concise and practical. But certainly not in Python. As mentioned in the previous section, numbers are natural immutable objects, and the so-called self-increase is self-increase, so it cannot increase itself. It can only point from one object to the next. You can write a + = 1 like this.
3. Since Python changes only reference relationships, how do you copy a list?
A = [1, 2, 3] b = a # this cannot copy a list, both an and b point to the list [1, 2, 3] # answer: # # 1. Use list's copy method b = a.copy () # # 2. Use the slice operation b = a [:] # slice operation to return a new object about how to implement the assignment logic in Python. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.
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.