演示了如何隐藏类的属性和方法来访问类外的隐藏变量
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)
Run Code Online (Sandbox Code Playgroud)
访问隐藏属性会导致错误,注释掉以下行以消除错误:
print (a.__hideAttr)
Run Code Online (Sandbox Code Playgroud)
要访问类的隐藏属性,请使用:
print (a._hiding__hideAttr)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13247 次 |
| 最近记录: |