访问python中类的隐藏属性

时间:2016-07-08 08:49:49

标签: python

演示了隐藏类和方法的属性以访问类

之外的隐藏变量的方法
class hiding():
    # class attribute, "__" befor an attribute will make it to hide
    __hideAttr = 10
    def func(self):
        self.__hideAttr += 1 
        print(self.__hideAttr)

a = hiding()
a.func()
a.func()


#AttributeError: 'hiding' object has no attribute '__hideAttr'
print (a.__hideAttr)

1 个答案:

答案 0 :(得分:0)

访问隐藏属性会导致错误,请注释掉以下行以删除错误:

print (a.__hideAttr)

要访问类的隐藏属性,请使用:

print (a._hiding__hideAttr)