python 类私有成员
2012-07-24 08:01
211 查看
Python中默认的成员函数,成员变量都是公开的(public),而且python中没有类似public,private等关键词来修饰成员函数,成员变量。
在python中定义私有变量只需要在变量名或函数名前加上 ”__“两个下划线,那么这个函数或变量就会为私有的了。、
运行结果:
test_1 is ok....
Traceback (most recent call last):
File "D:\Install\Eclipse\WorkSpace\Python\test_class.py", line 15, in <module>
test.__test_2()
AttributeError: Test instance has no attribute '__test_2'
在python中定义私有变量只需要在变量名或函数名前加上 ”__“两个下划线,那么这个函数或变量就会为私有的了。、
''' Created on 2012-7-24 @author: Administrator ''' class Test: def test_1(self): print 'test_1 is ok....' def __test_2(self): print 'test_2 is ok...' test = Test() test.test_1() test.__test_2()
运行结果:
test_1 is ok....
Traceback (most recent call last):
File "D:\Install\Eclipse\WorkSpace\Python\test_class.py", line 15, in <module>
test.__test_2()
AttributeError: Test instance has no attribute '__test_2'
相关文章推荐
- Python中定义“私有”成员变量和成员函数
- Python学习笔记--类简介,继承,私有成员
- Python中定义“私有”成员变量和成员函数
- Python模拟保护和私有成员
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- python之类私有成员
- Python基础(十二) 类私有成员和保护成员
- python中类的私有成员的 name managing
- Python类的私有成员
- Python中定义“私有”成员变量和成员函数
- Python 类简介,继承,私有成员
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- Python中定义“私有”成员变量和成员函数
- python私有成员
- 【C++】私有数据成员不能用对象去访问吗
- 2Python全栈之路系列之面向对象进阶及类成员
- 私有成员变量的访问(类内与类外)
- python中的类变量、成员变量和成员函数的局部变量使用