1.
Python Variables
if a single-valued variable is called a one-dimensional variable, and the variable that can extend the element is called a multidimensional variable, the python variable can be divided as follows:
Variable dimension |
Python Variables |
Description |
One-dimensional |
Digital |
int (signed integral type) |
Number types can be converted |
Long (longer integer [can also represent octal and hexadecimal]) |
Float (float type) |
Complex (plural) |
String |
Strings are rich in operators and inline functions; formatted output; |
Two-dimensional |
List |
In addition to Ganso, lists and dictionaries have the flexibility to extend elements |
Meta-group |
Dictionary |
2.
Assignment handling of Python variables
The main analysis Here is that the original variable is assigned to the new variable, the relationship between the two changes, such as: The original variable is varold, the new variable varnew = Varold, after varnew has changed, then Varold How are the addresses of the two changed? Here are the test cases:
Use Case 1:
def whole_modify (Value_old, value_new): #before obj_old = value_old obj_new = obj_ Old print (obj_old, ID (obj_old)) Print (obj_new, ID (obj_new)) #after obj_new = Value_new print (obj_old, ID (obj_old)) Print (obj_new, ID (obj_new)) print ( ' value: ' ) Whole_modify ( ' AAA ' , 9) print ( ' list: ' ) Whole_modify ([ ' A ' , 2], [3, ' B ' ]) print ( ' Dictionary: ' ) Whole_modify ({1: ' A ' , ' B ' : 2}, {3: ' C ' }) |
Test Results:
Value (' AAA ', 38126200) (' AAA ', 38126200) (' AAA ', 38126200) (9, 4014992) List ([' A ', 2], 43066072) ([' A ', 2], 43066072) ([' A ', 2], 43066072) ([3, ' B '], 41421080) Dictionary: ({1: ' A ', ' B ': 2}, 39583440) ({1: ' A ', ' B ': 2}, 39583440) ({1: ' A ', ' B ': 2}, 39583440) ({3: ' C '}, 39599104) |
The whole_modify in use Case 1 is the overall modification of the new variable, which is to assign a whole new data to the new variable directly with the = sign, and from the test case, the conclusion is:
regardless of the one-dimensional or two-dimensional variables, the new variable allocates memory after the whole = sign, and the value is the newly assigned value; The original variable remains unchanged.
Use Case 2:
print ( "list:" ) Obj_old = [ ' A ' , 2] Obj_new = obj_old print (obj_old, ID (obj_old)) Print (Obj_new, ID (obj_new)) #after Obj_new[0] = 3 print (obj_old, ID (obj_old)) print (obj_new, ID ( obj_new)) print ( "dictionary:" ) Obj_old = {1: ' a ' , ' B ' : 2} Obj_new = Obj_old print (obj_old, ID (obj_old)) Print (obj_new, ID (obj_new)) #after obj_new[ ' b ' ] = ' Newvaluestr ' Print (obj_old, ID (obj_old)) print (obj_new, ID (obj_new)) |
Test Results:
List ([' A ', 2], 39932832) ([' A ', 2], 39932832) ([3, 2], 39932832) ([3, 2], 39932832) Dictionary ({1: ' A ', ' B ': 2}, 40107728) ({1: ' A ', ' B ': 2}, 40107728) ({1: ' A ', ' B ': ' Newvaluestr '}, 40107728) ({1: ' A ', ' B ': ' Newvaluestr '}, 40107728) |
In use Case 2, the main thing is to modify the internal elements of the two-dimensional variables (one-dimensional variables without testing), from the test situation, the conclusion is:
When the second dimension variable is assigned to a new two-dimensional variable, the address of either of them is changed, but the value is modified synchronously.
3.
Analysis
Python 's variable assignment (including arguments, return parameters, and so on) is very different from C/+ + and It is difficult to summarize its usage in a sentence. The above two test cases are mainly from the variable type angle to distinguish, has the very strong representative, may extend to many scenes auxiliary comprehension.
Many people see python 's variable assignment as a reference, which should be problematic, such as in use case 1 above, which is obviously not a reference, because the new variable address and value change, and the old variable remains completely unchanged. the concept of a C + + Reference is that the new and old variable addresses do not change and the values are synchronized.
Use case 2 to make a partial modification of the two-dimensional variables, you will find that the new and old variables of the local elements of the address and value are synchronized changes (the variable itself address unchanged), this and reference the same place is the value of the synchronization changes, the difference is the variable address also synchronized changes.
This processing of Python should be based on memory efficiency considerations, although python application development does not take into account the variable address and memory recycling, but if the variable is assigned to the memory situation is completely ignored, It is easy to produce unexpected results.
A brief analysis of Python variable memory