python类的属性

一 介绍

1、在python中,如果属性是以双下划线开始的,则该属性是私有属性。
2、如果在类内部使用私有属性,则应该在私有属性前加上"self."。
 
二 代码示例
  1. >>>class book:
  2. __author =''
  3. __name =''
  4. __page =0
  5. price =0
  6. __press =''
  7. >>> a = book()
  8. >>> a.__author
  9. Traceback(most recent call last):
  10. File"<pyshell#9>", line 1,in<module>
  11. a.__author
  12. AttributeError:'book' object has no attribute '__author'
  13. >>> a.price
  14. 0
  15. >>> a.price =20
  16. >>> a.price
  17. 20
  18. >>> a.__name
  19. Traceback(most recent call last):
  20. File"<pyshell#13>", line 1,in<module>
  21. a.__name
  22. AttributeError:'book' object has no attribute '__name'
  23. >>> a.__page
  24. Traceback(most recent call last):
  25. File"<pyshell#14>", line 1,in<module>
  26. a.__page
  27. AttributeError:'book' object has no attribute '__page'
 

猜你喜欢

转载自cakin24.iteye.com/blog/2382640
今日推荐