python类中的私有方法

假设有如下一个python类:

class Foo(object):
def __
a(self):
print "Bet you can't see me..."

def bar(self):

        self.__a()

   

   

   

sSecurityp的一个实例,我们

s._Foo__a()

   

这种机制可以阻止继承类重新定义或者更改方法的实现,比如,定义一个Foo的派生类:

   

class Goo(Foo):

    def __a(self):

        print 'private method of Goo'

   

= Goo()

g.bar()

"Bet you can't see me..."   

self.__a()已自动变形为self._Foo__a(),Goo继承的bar()方法也是如此

   

   

   

原文地址:https://www.cnblogs.com/wuxiangli/p/10243808.html