Python 05 understanding of references in Python

Source: Internet
Author: User

The data's in-memory address is a reference to the data.
If the two variables are the same reference, then the data corresponding to the two variables must be the same;
if two variables correspond to the same data, the references are not necessarily the same.
The ID (data) can be used to see the corresponding address of the data, modify the value of the variable, in fact, the reference to modify the variable.
data can be divided into: mutable type and invariant type
Variable Type:
If you modify the contents of the data, the address of the data is not changed.
There are lists, dictionaries, set sets
Immutable types:
If you modify the contents of the data, the address of the data changes.
there are strings, tuples, numbers
When the Python interpreter starts for the first time, the small number ( -5~256) and the short string (less than 21) are cached into the buffer, which is taken directly from the cache when the numbers and strings are used in the program.
m =
n =
Print (ID (m)) # 1811121856
Print (ID (n))
data that is not in the range of small numbers or small strings is cached on first use .
m =
n =
Print (ID (m)) # 2345027009360
Print (ID (n)) # 2345027009360

Demo:
1.
variable A actually stores a reference (address) of 1 (A is compiled into an instruction during program execution)
A = 1
B = a
Print (ID (a)) # 1820749280
Print (ID (b)) # 1820749280
A = 2 # Modify the immutable type (reference)
Print (ID (a)) # 1820749312 A's ID has changed
Print (ID (b)) # 1820749280
print (A, b)

A = [1, 2,[3,5]]
b = A
Print (ID (a)) # 1758314288776
Print (ID (b)) # 1758314288776
# A.append (3)
A[0] = 6
A[2][0] = 1
B[2][1] = 6
Print (ID (a)) # 1758314288776 Note A and b always point to the same address
Print (ID (b)) # 1758314288776
Print (a) # [6, 2, [1, 6]]
Print (b) # [6, 2, [1, 6]]

2.

list = []
dict = {"Name": "Wangjie", "Age": +}
Print (ID (dict))
A = ten
List.append (dict) # Add a dict reference to the list 0x111 content is (pointing) {"name": "Wangjie", "Age": +}
List.append (a) # Add a reference to the list 0x222 content is (point)
print (list) # List[0] is 0x111, content is (point) data {"Name": "Wangjie", "Age": 23},list[1] Content 0x222, content is (point) data
a = 20 # modified the reference of A's value a changes 0x333
# but does not affect whether the reference in the list points to a value or to 0x111 pointing to {"name": "Wangjie", "Age": Max}
# and 0x222 point to ten
print (list)
dict["name"] = "Lili" #修改了dict的值 Dict is a mutable data type, dict reference does not change, but point changes 0x points to 0x111, but 0x222 has changed to {' Name ': ' Lili ', ' age ': 23}
the content of print (list) # List[0] is 0x111, which points to the data {' name ': ' Lili ', ' age ': 23},list[1] with 0x222 pointing to data

3.
list = []
list2 = []
m = Ten
def func ():
Global M
m =20
list2 = [up] # does not belong to the modification, the modification needs to pass the method, this is overriding the global variable List2
List.append (1) # Modified by Append method, the reference of list is not changed
print (LIST2) # [1, 2]
print (list) # []
print (M) # ten
Print (ID (m)) # 1811115776

func () # [1, 2]

Print (list) # [1]
Print (LIST2) # []
Print (m) # 20
Print (ID (m)) # 1811116096

4.

def log2 ():
#info_dict = {} # The dictionary that stores student information is overwritten with the new data if it is placed outside the for loop
info_list = []
num = input ("Please enter the number of information to be entered")
num = int (num)
For i in range (num):
info_dict = {} # re-assigned directly to Info_dict, info_dict reference changes the dictionary to store student information in for loop #
print ("Input%s bit information"% (i + 1))
name = input ("Enter name:")
id = input ("Input study number")
info_dict["name"] = name
info_dict["School Number" = ID
info_list.append (info_dict)
print (info_list)
For info in info_list:
for K, V in Info.items ():
print ('%s =%s '% (k,v))
log2 ()

5.

a = [1, 2]
B = [3, 4]
A.append (b) # [1,2,[3,4]]
B.append (a)
print (a) # [1, 2, [3, 4, [...] ]
print (b) # [3, 4, [1, 2, [...] ]

6. Pass the data, passing the corresponding address of the data.

a = [[]] * 5
print (a) [[],[],[],[],[]]
Print (ID (a)) # 2132205131400
Print (ID (a[0)) # 2132205131592
Print (ID (a[1)) # 2132205131592
a.append (1)
Print (ID (a)) # 2132205131400
print (a) # [[],[],[],[],[],1]
a[0].append (2)
Print (ID (a[0)) # 2132205131592
print (a) # [[2],[2],[2],[2],[2],1]
a[1].append (3)
Print (ID (a[1)) # 2132205131592
print (a) # [[2,3],[2,3],[2,3],[2,3],[2,3],1]

Python 05 understanding of references in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.