类的私有属性

前面带有两下划线代表是私有属性,能在类的内部调用,不能在类的外部调用,示例:

class money:
    __self = 50
    all = 300
    def giveS(self):
        print('给钱',self.__self)
    def giveA(self):
        print('给钱',self.all)
a = money()
a.giveS()
a.giveA()

输出:

给钱 50
给钱 300

运行

print(a.__self)

输出报错:

AttributeError                            Traceback (most recent call last)
<ipython-input-54-d67a9ccaa360> in <module>
----> 1 print(a.__self)

AttributeError: 'money' object has no attribute '__self'

运行

print(a.all)

输出:

300
原文地址:https://www.cnblogs.com/daicw/p/13050429.html