2. python variables and assignments; 2. python variable assignments
First of all, let's stop talking about how to use variables. Here we will introduce the command rules of variables and the memory behavior of variable assignment.
1. variable naming rules
The variable actually calls the value in memory through a tag, and the variable name is the name of the tag, but in case the tag has been occupied in advance or the interpreter deems it illegal, then an error is reported. The naming rules of variables are summarized below:
1. Keywords of python cannot be used, that is, tags cannot be occupied in advance. The keywords of python include:
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
2. variable nameFirst characterCannot beNumber,Such a name is considered invalid.
3. variable nameOnlyYesLetter,NumberOrUnderlineAny combination.
Rules are rules. There is no explanation, but we can make further requirements on the premise that they comply with the rules. For details, refer to the pep8 encoding specification of python.
2. Variable assignment
In python, when assigning values to variables, A equals sign (=) is used. Note that not two (=) are used ).
For example:
a = 10
The action of this Code is equivalent:
Note: we do not create a value for Mark a. Instead, we create a value first and then point it with a tag.
After knowing how to assign values, let's look at the following code:
a = 10 b = aa = 20
So, B =?
If we follow the common mathematical thinking, B is definitely equal to 20, but this is not the case here. If we follow the general thinking, it is as follows:
According to this logic, B is definitely equal to 20, but it is wrong, so I drew a cross.
Because a variable is a tag used to call the value in memory, when we assign a tag to another tag, it should be directed to the memory of another tag, rather than to the tag.
According to this principle, our idea should be as follows:
That is to say, B should be equal to 10 at this time. Let's test whether it is correct:
The verification is correct and there is no problem with this idea.
3. Garbage collection mechanism in python
At this time, we may have questions about the following situation:
a = 10a = 20
According to our previous thinking, a has already pointed to 20. What about the remaining 10?
This is about the garbage collection mechanism in python. python recycles junk data in the memory according to certain rules to release the memory space.
The cornerstone of Python memory collection is reference count. "When an object's reference is created or copied, the reference count of the object is incremented by 1. When an object's reference is destroyed, object reference count minus 1 ". If the object reference count is reduced to 0, the memory occupied by the object is released..
In fact, I am not very familiar with this part, so I can only talk about it. There may be errors. If you want to continue to understand it in depth, you can view some articles of others or look at official documents.
Python clears the data with a reference count of 0 from the memory. The specific recovery time is not clear, and may be recycled at intervals.
A simple garbage collection mechanism is much more complicated than a garbage collection mechanism, because sometimes circular references may occur, it is not enough to clear 0 references, so there are other algorithms to reclaim them.
I do not know much about garbage collection, because I have not learned so deeply. I will continue to add more information as to how to learn it.