你如何在Python中正确使用私有函数?

时间:2013-08-04 02:20:12

标签: python function private

我是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'

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您在代码的多个部分中混淆了self.matrixself._matrixself_matrix。最有可能的是,您的意思是self.matrixself._matrix,其他人则是拼写错误。另外,fmatrixSort可能应该在__loadVec上调用__savetoMatrixself,而不是正在进行的调用。

附加说明:

如果您没有要返回的值,则在函数末尾不需要return。当执行到达函数末尾时,函数会自动返回。

range可以通过3种方式调用:

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

如果您要将范围设为0,请忽略start参数并使用1参数调用range

答案 1 :(得分:2)

Python并不具备private函数的概念。但是,它确实会处理名称以至少两个下划线开头并且最多只有一个下划线的类属性 - 它会破坏名称以使它们稍微难以访问。在此示例中,您可以看到函数__func2的名称已被破坏。它仍然可以访问和调用该函数 - 但是您必须特别努力才能执行此操作,只需调用o.func2()失败:

james@bodacious: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()
james@bodacious: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'
james@bodacious:tmp$

所以回答你提出的问题:

  

如何在Python中正确使用私有函数?

答案是:就像任何其他功能一样,但你必须知道错位的名字。

继续讨论你想问的问题:

AttributeError: matrix instance has no attribute '_matrix'

这来自第154行:

self._matrix.__loadVec(vec,res)

错误消息告诉您名为self的对象是类matrix的实例;但它没有名为_matrix的属性。参考上面的__savetoMatrix函数,看起来该属性只是简称为matrix - 因此您需要将其称为self.matrix(“matrix的属性名为self)的对象。

__savetoMatrix函数引用self.matrix而不是self._matrix

然而,这里存在更深层次的问题。在行之间阅读,看起来这个代码来自一个名为matrix的类;并且该类的实例具有名为matrix的属性。当您致电self.matrix.__loadVec()时,您正在调用名为__loadvec()的函数,该函数绑定到绑定到名为matrix的对象的属性self

即使这是你想要做的,这也不会起作用,因为如上所述的名称修改 - 假设名为matrix的属性具有类inner_matrix,你必须调用函数为self._matrix._inner_matrix__loadVec()

认为你实际上要做的是调用类__loadVec()中定义的名为matrix的方法。为此,您只需致电self.__loadVec()即可。因为它是对同一个类中的函数的调用,所以您甚至不需要担心名称错误 - 这些函数旨在在类中使用,并且解释器将为您处理错误。

james@bodacious: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()
james@bodacious:tmp$python test.py
['__doc__', '__module__', '_myclass__func2', 'func1', 'func3']
two
three