Initialization Python dictionary, dictionary parameters passed by reference and other issues

# Initialize method of a dictionary: 

1 a = {}
2 b, c = {}, {}
3 d =dict(a="a", b="b")  # b = {"a": "a", "b": "b"}
View Code

 

# Method two: initialization function by global

. 1  DEF Change ():
 2      Global a, b, c    # completely modify a, b, c to use global, the key portion does not need to modify the Global 
. 3      A = { " . 1 " : " . 1} 
. 4      B = { " 2 " : " 2} 
. 5      C = { " . 3 " : " . 3 " }
 . 6  
. 7 A, B, C = {}, {}, {}
 . 8  Change ()
 . 9  
10  # printing 
. 11 >> A = { " . 1 " :"1}
12 >> b = {"2": "2}
13 >> c = {"3": "3"}
View Code

 

# Method three: initializes the value returned by a function

1 def change():
2     a = {"1": "1"}
3     return a
4 
5 a = change()
6 
7 # 打印
8 >> a = {"1": "1"}
View Code

 

# Wrong way: completely revised dictionary by reference parameter passing

. 1  DEF Change (A):    # pass a reference parameter, is intended to cover the original dictionary as new dictionary 
2      A = { " . 1 " : " . 1 " }
 . 3  
. 4 A = {}
 . 5  Change (A)
 . 6  
. 7  # Printing 
8 > > A = {}     # not successfully modified
View Code

 

# This bug for a long time only to find, with c / c ++ reference mass participation is not entirely correct to understand python

Guess you like

Origin www.cnblogs.com/yangwu-183/p/12567173.html