一 介绍
1、在python中,如果属性是以双下划线开始的,则该属性是私有属性。
2、如果在类内部使用私有属性,则应该在私有属性前加上"self."。
二 代码示例
>>>class book:
__author =''
__name =''
__page =0
price =0
__press =''
>>> a = book()
>>> a.__author
Traceback(most recent call last):
File"<pyshell#9>", line 1,in<module>
a.__author
AttributeError:'book' object has no attribute '__author'
>>> a.price
0
>>> a.price =20
>>> a.price
20
>>> a.__name
Traceback(most recent call last):
File"<pyshell#13>", line 1,in<module>
a.__name
AttributeError:'book' object has no attribute '__name'
>>> a.__page
Traceback(most recent call last):
File"<pyshell#14>", line 1,in<module>
a.__page
AttributeError:'book' object has no attribute '__page'