Reference to variables
- Variables and data are stored in memory.
- In python, the parameter transfer and return value of a function are passed by reference.
The Concept of Function Reference
In python
- Variables and data are stored separately.
- Data is stored in a location in memory.
- Variables hold the address of the data in memory.
- The address of the data recorded in a variable is called a reference.
- Using the id() function, you can view the memory address where the data is stored in the variable.
Note: If a variable has been defined, when assigning a variable, it essentially changes the reference of the data; that is, the variable no longer refers to the previous data; and the variable changes to the data reference of the new assignment.
a = 1 id(a) 140721952793280 id(1) 140721952793280 b = a id(b) 140721952793280 a = 2 id(a) 140721952793312 id(b) 140721952793280 b = a id(b) 140721952793312 b = 2 id(b) 140721952793312
Function Reference Understanding
We can understand the name of the variable as a note paper, and the variable name and data are equivalent to sticking the note paper on the data.
When we are a = b, we paste a and B labels on the same data. If we reassign a, we tear down a's note and paste it on another data, but the position of b's note remains unchanged.
The relationship between function passing and reference
The transfer of function parameters is actually the reference of the corresponding parameter variables, rather than the data saved by the parameters.
def test(num): print("Inside the function%d The corresponding memory address is%s" % (num, id(num))) a = 10 print("a The memory address where the variable holds data is %s" % id(a)) test(a) # The memory address of a variable to save data is 140722085962720 # The memory address corresponding to 10 inside the function is 140722085962720.
Function Return Value and Reference
The return value of a function is also a reference to the return variable, not the real data.
Data address is essentially a number.
def test(num): result = "test_password" print("Return value in function result The memory address is %s" % id(result)) return result a = 10 r = test(a) print("Returned %s The memory address is %s" % (r, id(r))) # The memory address of the return value result in the function is 23333111002800 # The memory address of the test_password returned is 23333111002800
Variable and Invariant Types
Modifying the variable type is to modify the content of the data without modifying the address of the variable reference.
Reassignment modifies the address of the variable reference;
Invariant type, data in memory is not allowed to be modified:
- Number type;
- Tuple;
- Character string;
Variable type, data in memory can be modified:
- List;
- Dictionaries;
The effect of variable type modification and reassignment on references
Variable types, such as lists and dictionaries, do not affect the memory addresses of references when modifying their data.
Only when we reassign variables can we influence references.
Let's just give an example of a list. Like a dictionary, there's no need to go into it.
# Effects of List Data Modification and Reassignment on References a = [1,2,3] id(a) 1956997579272 a.append(4) a [1, 2, 3, 4] id(a) 1956997579272 a.remove(2) a [1, 3, 4] id(a) 1956997579272 a.clear() a [] id(a) 1956997579272 a = ['a','s','d'] id(a) 1956997945160
Dictionary key s can only use immutable types.
Note: Variable types of data changes are achieved through methods.
Hash algorithm
d = {} d["name"] = "zhangsan" d {'name': 'zhangsan'} d[1] = "integer" d {'name': 'zhangsan', 1: 'integer'} d[(1,)] = "tuple" d {'name': 'zhangsan', 1: 'integer', (1,): 'tuple'} d[[1,2,3]] = "list" Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unhashable type: 'list' d[{"age":18}] = "Dictionaries" Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unhashable type: 'dict'
- Python contains a function called hash(o), which receives an immutable type of data as a parameter and returns an integer.
- Hashing is an algorithm that extracts the signatures of data (fingerprints); the same data gets the same results, different data gets different results;
- In python, when setting the key value pair of a dictionary, the key is hash first to decide how to save the data of the dictionary in memory, so as to facilitate the subsequent dictionary addition, deletion and modification.
- The key of a dictionary key-value pair must be immutable data, and the value of a key-value pair can be any data type.
Hash algorithm can only hash immutable type.
Because dictionary keys use hashing, dictionary keys can only be immutable types.
hash(1) 1 hash("hello") 2061306992742373012 hash("hello python") 9189581639312291988 hash((1,2)) 3713081631934410656 hash([1,2]) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unhashable type: 'list' hash({"age":18}) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unhashable type: 'dict'