2013-08-04 51 views
1

我是Python的新手,當涉及到私有函數時,我正面臨一些問題。我想在一個公共方法中調用其中的兩個,只是爲了讓代碼看起來很清晰,但我根本無法理解運行時錯誤顯示的內容。下面是完整的代碼有問題的一部分:如何在Python中正確使用私有函數?

def __loadVec(self,vec,res): 
    for i in range(0,res.getRows()): 
     for j in range(0,res.getColumns()): 
      vec.append(self.matrix[i][j]) 
    return 

def __savetoMatrix(self,vec,res): 
    index = 0 
    for i in range(0,res.getRows()): 
     for j in range(0,res.getColumns()): 
      self.matrix[i][j] = vec[index] 
      index += 1 
    return 

def fmatrixSort(self,res): 
    try: 
     print "Sorting matrix information..." 
     vec = [] 
     self._matrix.__loadVec(vec,res) 
     vec.sort() 
     self_matrix.__savetoMatrix(vec,res) 
    except TypeError: 
     print "TypeError in fmatrixSort"    
    return 

我想要做的是這樣,因爲它與最低值開始,並以最高的結束完全組織的矩陣。

這就是程序顯示錯誤:

Traceback (most recent call last): 
    File "MatrixClass.py", line 211, in <module> 
    main() 
    File "MatrixClass.py", line 203, in main 
    mat.fmatrixSort(res) 
    File "MatrixClass.py", line 154, in fmatrixSort 
    self._matrix.__loadVec(vec,res) 
AttributeError: matrix instance has no attribute '_matrix' 

我應該如何解決這個問題?

+0

請勿在Python中使用類專用的__identifiers。它們的存在原因與Java/C++中的私有成員完全不同,並不能替代它們。 – msw

+0

你的代碼是borken,什麼是'self_matrix';你的問題也不是由__methods引起的,而是缺少self._matrix屬性! –

回答

2

Python並不完全具備private函數的概念。但是,它的確處理了類名屬性,其名稱以至少兩個下劃線開頭,並且最多隻有一個下劃線 - 特別是它的名稱 - 使它們更難以訪問。在這個例子中,你可以看到函數__func2的名字已經損壞。它仍然可以訪問並調用該函數 - 但你必須做出特別的努力去做,只是要求o.func2()失敗:

[email protected]:tmp$cat test.py 

class myclass: 

    def func1(self): 
     print "one" 

    def __func2(self): 
     print "two" 

o = myclass() 

print dir(o) 

o._myclass__func2() 
o.func2() 
[email protected]:tmp$python test.py 
['__doc__', '__module__', '_myclass__func2', 'func1'] 
two 
Traceback (most recent call last): 
    File "test.py", line 15, in <module> 
    o.func2() 
AttributeError: myclass instance has no attribute 'func2' 
[email protected]:tmp$ 

因此,要回答你問的問題:

怎麼辦你在Python中正確使用私有函數?

答案是:就像任何其他函數一樣,但是你必須知道這個損壞的名字。

移動到這個問題你要問:

AttributeError: matrix instance has no attribute '_matrix' 

這是從行來154:

self._matrix.__loadVec(vec,res) 

錯誤消息告訴您該對象調用self是一個實例matrix;但它沒有稱爲_matrix的屬性。參考上述__savetoMatrix功能,它看起來像屬性被簡單地稱爲matrix - 所以你需要把它稱爲self.matrix(「屬性稱爲叫self對象的matrix

這本__savetoMatrix函數引用self.matrix相當。 ,比self._matrix

但是,這裏有一個更深層次的問題的字裏行間,它看起來好像這個代碼來自一個名爲matrix類;與類的實例有一個屬性,叫做matrix當你調用self.matrix.__loadVec(),你正在調用這個函數稱爲__loadvec(),其綁定到綁定到名爲self的對象的屬性matrix

即使這是你想要做上面提到的,這會不會是因爲名字改編的工作是什麼 - 假設屬性稱爲matrix有類inner_matrix,你必須調用函數爲self._matrix._inner_matrix__loadVec()

認爲你實際上想要做的是調用類matrix中定義的名爲__loadVec()的方法。要做到這一點,你只需要撥打self.__loadVec()。因爲它是對同一個類中的函數的調用,所以你甚至不需要擔心名字的變形 - 這些函數是打算在類中使用的,並且解釋器會爲你處理變形。

[email protected]:tmp$cat test.py 

class myclass: 

    def func1(self): 
     print "one" 

    def __func2(self): 
     print "two" 

    def func3(self): 
     self.__func2() 
     print "three" 

o = myclass() 
print dir(o) 
o.func3() 
[email protected]:tmp$python test.py 
['__doc__', '__module__', '_myclass__func2', 'func1', 'func3'] 
two 
three 
+0

感謝詹姆斯,它工作完美 –

+0

你說:「Python不完全具有私人功能的概念。」如果你停在那裏,這將是一個更好的答案。 – msw

+0

不同意;朱利安的根本問題與私人職能無關,這只是一個分心,必須處理,以便我們能夠解決真正的問題。 –

2

您已將self.matrix,self._matrixself_matrix混淆在您的代碼的多個部分中。很有可能,你的意思是self.matrixself._matrix,其他人是拼寫錯誤。另外,fmatrixSort應該可能在self上調用__loadVec__savetoMatrix,而不是它當前正在執行的操作。

其他注意事項:

你不需要return在函數結束時,如果你沒有一個價值迴歸。當執行遇到函數結束時,函數自動返回。

range可以稱得上是3種方式:

range(stop) 
range(start, stop) 
range(start, stop, step) 

如果你打算在0開始的範圍內,就離開了start參數並調用range 1個說法。

+0

謝謝,我沒有意識到我可以做到這一點 –