Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python 类私有成员

Python中默认的成员函数,成员变量都是公开的(public),而且python中没有类似public,private等关键词来修饰成员函数,成员变量。在python中定义私有变量只需要在变量名或函数名前加上 ”__“两个下划线,那么这个函数或变量就会为私有的了。
  1. """"" 
  2. Created on 2012-7-24 
  3.  
  4. @author: Administrator 
  5. """  
  6. class Test:  
  7.     def test_1(self):  
  8.         print "test_1 is ok...."  
  9.           
  10.     def __test_2(self):  
  11.         print "test_2 is ok..."  
  12.   
  13. test = Test()  
  14. test.test_1()  
  15. test.__test_2()  
运行结果:test_1 is ok....Traceback (most recent call last):File "D:InstallEclipseWorkSpacePython est_class.py", line 15, in <module>test.__test_2()AttributeError: Test instance has no attribute "__test_2"