Document directory
- Example 1: integer Transfer
- Example 2: Variable Transfer
- Example 3: Address storage of the List (for personal understanding, the source code is not found. If any error occurs, please correct it)
- Example 4: List transfer (reference)
- Example 5: list assignment
- Example 6: dictionary reference
- Example 7: dictionary assignment
- Example 8: transfer different types of Variables
In python, the assignment and application are not clearly distinguished,Generally, the transmission of static variables is a value, and the transmission of dynamic variables is a reference.(Note: it is also an application for the first transmission of static variables. When you need to modify static variables, because static variables cannot be changed, you need to generate a new storage space) I personally think it is easier to distinguish rules clearly.
String, integer, and tuples are static variables
List. The dictionary is a dynamic variable.
The following example shows how to use the ID () function to display the variable memory address in the python 2.7 environment.
Example 1: integer Transfer
A = 4
Print ID ()# ID function display variable memory address
B =
Print ID (B)
B = 6
Print ID (B)
>>>
19691376
19691376
19691328
Description,
1. A is assigned 4 values.
2. B is a reference of.
3. B changed. Because B points to a static variable 4 at this time, when assigning values to B, B first saves 6 to a different address from 4, and then B points to the address 6.
Example 2: Variable Transfer
A = "mm"
Print ID ()
B =
Print ID (B)
B = "Nn"
Print ID (B)
>>>
37313312
37313312
37313352
Note:
1. A is a variable name pointing to a string.
2. When a is passed to B, B is a reference pointing to the same address as.
3. When B is assigned a new string value, because B points to "mm" as a static variable, a new space is generated to store "Nn", and B points to "Nn"
Example 3: Address storage of the List (for personal understanding, the source code is not found. If any error occurs, please correct it)
- A = [4, 5]
- Print "address a =", ID ()
- Print "address a0 =", ID (A [0])
- Print "address a1 =", ID (A [1])
- B =
- B [0] = 6
- Print "A =",
- Print "B =", B
- Print "address a =", ID ()
- Print "address B =", ID (B)
- Print "address a0 =", ID (A [0])
- Print "address a1 =", ID (A [1])
- B [1] = "str"
- Print "A =",
- Print "B =", B
- Print "address a =", ID ()
- Print "address B =", ID (B)
- Print "address a0 =", ID (A [0])
- Print "address a1 =", ID (A [1])
>>>
- Address a = 24415912
- Addresses a0 = 23648112
- Address a1 = 23648088
- A = [6, 5]
- B = [6, 5]
- Address a = 24415912
- Addresses B = 24415912
- Addresses a0 = 23648064
- Address a1 = 23648088
- A = [6, 'str']
- B = [6, 'str']
- Address a = 24415912
- Addresses B = 24415912
- Addresses a0 = 23648064
- Address a1 = 140688190862360
Note:
From lines 1, 2, and 3 of the output, we can see that the address of a> the address of a [0]> the address of a [1, in addition, the addresses a [0] And a [1] are far different. Therefore, the list in python is definitely not an array in C and should be a linked list. (You can also see from the python list that has the append () function)
When line A of the command is passed to line B, lines 6 and 7 show that a and B point to the same address, B is a reference of a,, the value of B should be a pointer to the linked list header.
The value of B [0] Changes in the command line 6th. From the output of lines 4 and 5, the content of A and B changes at the same time, from the output of Line 6 and line 7, B points to the same address. From the output of 7 rows, the address of a [0] has changed. Therefore, the first node of the linked list should be replaced (even if the variable type is compatible ), therefore, you can understand why Python does not focus on variable types. The address of output 8th behavior a [1] is not changed
Line 2 of the command, B [1] is changed. The analysis is the same as above. The tail element of the same linked list pointed to by A and B is replaced.
Example 4: List transfer (reference)
A = [4]
Print ID ()
B =
Print ID (B)
B [0] = 6
Print ID (B)
>>>
43351720
43351720
43351720
Description;
Same as above
Example 5: list assignment
A = [4]
Print ID ()
B = A [:]
Print ID (B)
B [0] = 8
Print ID (B)
>>>
22863528
23030888
23030888
Note:
1. A points to a dynamic variable list
2. B obtains a copy of list a stored in a new space, and B points to a new address (the difference between B = A and B = A [:] can be seen from, B =, B =)
3. The first element of B is assigned 8 values, which does not affect.
Example 6: dictionary reference
A = {"I": 99}
Print ID ()
B =
Print ID (B)
B ["O"] = 8
Print ID (B)
Print "A =",
Print "B =", B
>>>
28841616
28841616
28841616
A = {'I': 99, 'O': 8}
B = {'I': 99, 'O': 8}
Note:
1. A points to a dynamic variable
2. B is a reference of.
3. badds one to two, and a also adds
Example 7: dictionary assignment
A = {"I": 99}
Print ID ()
B = A. Copy ()
Print ID (B)
B ["O"] = 8
Print ID (B)
Print "A =",
Print "B =", B
>>>
16033424
16063424
16063424
A = {'I': 99}
B = {'I': 99, 'O': 8}
Note:
1. A points to a dynamic variable
2. B is a copy of a, pointing to a new address
3. badds a new variable, and a remains unchanged.
Example 8: transfer different types of Variables
A = "mm"
Print ID ()
B =
Print ID (B)
B = 8
Print ID (B)
Print "A =",
Print "B =", B
>>>
11687096
11687096
10692368
A = mm
B = 8
Note:
1. A points to a static
2. B is an application of a, pointing to the same address as
3. B has changed and points to a new data address.