python类中的隐藏属性

2024-10-01 15:49:34 发布

您现在位置: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)

Tags: to方法selfanmake属性itattribute

热门问题