|
Python is written in C language developed in the C language is no concept of a string, only the characters and an array of characters, usually represented by an array of characters strings, so the definition of a string in Python when, in fact, opened an in-memory space,
E.g:
Define a string string1 = hello ---------> 5 characters
An array of characters [ 'h', 'e', 'l', 'l', 'o'], which is stored in memory location: [ 'h', 'e', 'l', 'l', 'o']
And if you want to modify the string is hellosb, the need to re-open a block of data in memory space: [ 'h', 'e', 'l', 'l', 'o', 's',' b ']
Also: If you want to modify the hello string to hello0, then open up in memory space [ 'h', 'e', 'l', 'l', 'o', '0']
Also, if the string concatenation:
"Hello" + 'sb' + "alex"
Principle in memory can be understood as:
First, open up an address space to store 000001: "hello"
Because "hello" + 'sb', so to open up again in the next piece of memory storage space 000002 "hello" + 'sb'
Because "hello" + 'sb' plus another string "alex" behind
It also opened a store address space 000003 "hello" + 'sb' + "alex"
In summary:
If you want to achieve three strings "hello" + 'sb' + "alex" added the need to open up three address spaces
000001 --------> "hello"
000002 ---------> "hello" + 'sb'
000003 ---------> "hello" + 'sb' + "alex"
Note: The above principle also applies to C # and JAVA
Or more extra space in front of the C language can be automatically released, C # and JAVA is a high-level language, they are assigned to free up additional space in front of the garbage collection mechanism through the virtual machine (for example: 000001,000002)
summary:
Python interpreter executes the file through stages:
1, the memory load ---> lexical analysis ---> syntax analysis ---> Production compiled into byte code ---> virtual machine interpreter into machine code and then run to the CPU
2, three coding:
ascii: only represent 256 characters, because it is represented by 8 bits, or 8 2 ^ 256 possible
Unicode: a minimum of 16 bits to represent a character, that is a minimum of two bytes to represent characters
utf8: use utf8 reason is because some of the characters in the Unicode representation with eight since you can, and use 16 to represent it appears that waste, so use utf8, it can be said utf8 is an improved version of the Unicode
3, the script parameter [Get parameters sys module's argv function]
4, byte code: the suffix .pyc files, if you import a file, and execute it, then the file is imported automatically generate .pyc file; if the file is imported in addition to its .pyc generated content is the same, during the execution of a higher priority than .pyc file is imported (.py), if the file is imported to make changes, you need to recompile and then generate .pyc files.
5. Affirms variables Note:
1)Python variables can only be numbers, letters, and underscores
2)Beginning in Python variable can not be a number
3)Python variable name can not be specified in a keyword system
6, variable assignment
If after variable assignment and then assign variables to be, you need to re-open in memory address space segment, the C language is concerned there is no string, C language string representing the array of characters; why not let the string dynamic changes, and every time you want to assign? Python array represents an array of characters in memory address when it is continuous, if not continuous, then the latter address will empty out, that for a variable length of the unknown, the address to be vacated later if we do not know , and it is unpredictable, so Python variable addresses are continuous. In solving this problem, there are internal Python buffer pool or pond or called digital string pool concept, it has a Python space it will go through its own calculations, the numbers that you use frequently put it inside the region, when we use this frequently used strings to be avoided at a time to open up space in memory.
For the small number of buffer space: -5 to 257
For large numbers of buffer space 8 to 1000 |
|
|
|