access hidden attributes of a class in python
本问题已经有最佳答案,请猛点这里访问。
demonstrates的方式隐藏的属性和方法访问类的隐变量在类的外面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 | print (a.__hideAttr) |
要访问类的隐藏属性,请使用:
1 | print (a._hiding__hideAttr) |