私有属性

 1 class Women():
 2     def __init__(self,name):
 3         # 名字
 4         self.name = name
 5 
 6         # 年龄
 7         self.__age = 18
 8 
 9     def __secret(self):
10         print("%s的年龄是:%d" % (self.name,self.__age))
11 
12 
13 xf = Women("小芳")
14 
15 
16 print(xf.__age)
17 print(xf.__secret())
18 
19 # 如果要查看私有属性的方法----不推荐
20 print(xf._Women__age)
21 xf._Women__secret()
Traceback (most recent call last):
  File "E:/python_work/812/私有属性.py", line 16, in <module>
    print(xf.__age)
AttributeError: 'Women' object has no attribute '__age'

  

原文地址:https://www.cnblogs.com/yifengs/p/11345792.html