Python设置私有属性

python私有方法和私有属性

python的默认成员函数和变量都是公开的,python没有publicprivate等关键字,在python中用双下划线作为变量名前缀就变成私有的了

class Test(object):
    #普通方法
    def test(self):
        print("普通方法test")
    #普通方法
    def _test1(self):
        print("普通方法_test1方法")
    #私有方法
    def __test2(self):
        print("私有方法__test2方法")
 
t = Test()
t.test()
t._test1()
#t.__test2()#调用的时候会报错

posted @ 2024-08-06 21:07  黑鹿kuroshika  阅读(71)  评论(0)    收藏  举报