假设有如下一个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()方法也是如此

 
 

 
 

 
 

版权声明:本文为wuxiangli原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wuxiangli/p/10243808.html