python中的下划线规则测试

class Rules():

    __spe__ = "special"#特殊变量
    __private = 'private' #完全私有子类无法访问
    _halfprivate = 'halfprivate' #只能在类中模块中使用

    def _halfprivatefunc(self):
        print('half func')
    def __privatefunc(self): #完全私有方法
        print('private func')
    def __spec__(self):
        print('spec func')
    def test(self):
        print(self.__private)
        print(self.__privatefunc())
    @staticmethod
    def staticfunc():
        print(Rules._halfprivate)
class son(Rules):
    def test(self):
        super()._halfprivatefunc() # 子类中可以调用
r = Rules()
r.test()
print(r.__spe__) # 内置特殊变量,并不是私有,实例可以直接访问
print(r._halfprivatefunc()) #私有方法,虽然是私有但是还是可以访问,只是一种约定
r.__spec__() #内置特殊方法
s = son()
s.test()

  

原文地址:https://www.cnblogs.com/alplf123/p/8182567.html