2015-02-11 66 views
3

我有一個班級(self.d1)中聲明的字典。 調用F1功能後,self.d1必須更新到本地字典裏面聲明F1Python - 更新班級功能中的班級自我詞典

import copy 

class num: 
    def __init__(self): 
     self.d1 = {'a':1, 'b':2, 'c':3} 
     self.f1(self.d1) 

     print self.d1 

    def f1(self,d): 
     d2 = {'d':4, 'e':5, 'f':6} 
     d = copy.deepcopy(d2) 

test = num() 

我期望的輸出是:

{'d':4, 'e':5, 'f':6} 

但輸出

{'a':1, 'b':2, 'c':3} 

我想了解問題所在,而不僅僅是解決方案

回答

3

你不想指定df1(),因爲失去了舊的綁定它必須self.d1。所以在作業d之後只是局部變量f1()

但是你可以實現你想要有什麼用:

class num: 
    def __init__(self): 
     self.d1 = {'a':1, 'b':2, 'c':3} 
     self.f1(self.d1) 

     print self.d1 

    def f1(self,d): 
     d2 = {'d':4, 'e':5, 'f':6} 
     d.clear() 
     d.update(d2) 

test = num() 

輸出

{'e': 5, 'd': 4, 'f': 6} 

請注意,我的代碼使得f1()沒有分配到d,它不僅使調用,發生變異現有的對象。

有關此&相關主題的進一步參考,請參閱通過這樣偉岸,斯內德爾德這個優秀的文章:Facts and myths about Python names and values

0

你是公關oblem是與

d = deepcopy(...) 

你不改變字典,d提到,你只需要改變d參考另一個字典(在這種情況下,一本字典的新創建的副本)。

+0

所以有反正改變原來的字典d被提到? – 2015-02-11 12:49:55

+0

是的 - 請參閱@PM-2Ring的答案 – 2015-02-11 12:54:25

0

如果值{'a' : 1}分配給一些變量self.d1那麼該變量保持對價值的參考。這意味着您可以通過訪問它來更改d1的值,例如:self.d1['a'] = 2,現在值將爲{'a' : 2'}

您也可以通過將變量self.d1分配給新的東西來更改引用。因此,在您的功能f1中,您實際上更改d指向的引用,而不是其所指的值。由於功能範圍的限制,self.d1仍然會保留對功能範圍之外的原始值的引用。

0

Yust的其他解釋...

class num: 

    def __init__(self): 

     self.d1 = {'a':1, 'b':2, 'c':3} 
     # calling a function with a dictionary copies the *reference* to the 
     # dictionary object. 

     print 'first test ..' 
     self.test_dict_arg_1(self.d1) 
     print self.d1 

     print 'second test ...' 
     self.test_dict_arg_2(self.d1) 
     print self.d1 

    def test_dict_arg_1(self, d): 

     d2 = {'d':4, 'e':5, 'f':6} 
     # now you load d with a new value, the referenced object is untouched 
     d = d2 

    def test_dict_arg_2(self, d): 

     d2 = {'d':4, 'e':5, 'f':6} 
     # now you work with the referenced object 
     d.clear() 
     d.update(d2)