class Foo():
def __init__():
...
def public_method():
print 'This is public method'
def __fullprivate_method():
print 'This is fullprivate_method'
def _halfprivate_method():
print 'This is halfprivate_method'
f = Foo()
f.public_method() # OK
f.__fullprivate_method() # Error occur
f._halfprivate_method() # OK
f._Foo__fullprivate_method() # OK
class A(object):
def __init__(self):
self.__private()
self.public()
def __private(self):
print 'A.__private()'
def public(self):
print 'A.public()'
class B(A):
def __private(self):
print 'B.__private()'
def public(self):
print 'B.public()'
b = B()
>>> print '\n'.join(dir(A)) _A__private __init__ public >>> print '\n'.join(dir(B)) _A__private _B__private __init__ public
class ____(object):
def __init__(self):
self.__method()
def __method(self):
print '____.__method()'
print '\n'.join(dir(____))
__class__
__delattr__
__dict__
__doc__
__getattribute__
__hash__
__init__
__method # 没被矫直
__module__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__str__
__weakref__
obj = ____()
____.__method()
obj.__method() # 可以外部调用
____.__method()
class A(object):
def __init__(self):
self._A__private() # 这行变了
self.public()
def _A__private(self): # 这行也变了
print 'A.__private()'
def public(self):
print 'A.public()'
class C(A):
def __init__(self): # 重写 __init__ ,不再调用self._A__private
self.__private()# 这里绑定的是 _C_private
self.public()
def __private(self):
print 'C.__private()'
def public(self):
print 'C.public()'
c = C()
答案:
C.__private()
C.public()
class A(object):
def __init__(self):
self._A__private() # 调用一个没有定义的函数,但可执行
self.public()
def __private(self):
print 'A.__private()'
def public(self):
print 'A.public()'
a = A()
答案:
A.__private()
A.public()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有