1. 访问安全,其实也没有决定的安全安全
>>> class humer(object): ... def __init__(self, name): ... self.name = name ... def num(self): ... print self.name ... def __inner(self): ... print 'this is inner'
此时访问__inner方法只能这样this
>>> p._humer__inner()
this is inner
不然会报错spa
2. 在子类继承父类时,若是有相同的方法和变量的话,而咱们想使用各自的方法和变量。此时就可使用私有方法code
class Foo(object): def __init__(self): self.__baz = 42 def foo(self): print self.__baz class Bar(Foo): def __init__(self): super(Bar,self).__init__() self.__baz = 21 def bar(self): print self.__baz >>> b = Bar() >>> b.foo() 42 >>> b.bar() 21 #若是没有使用私有方法的话,那么foo和bar的值将都是子类的值