概述
Python中默认的成员函数,成员变量都是公开的(public),而且python中没有类似public,private等关键词来修饰成员函数,成员变量。
在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:InstallEclipseWorkSpacePythontest_class.py", line 15, in <module>
test.__test_2()
AttributeError: Test instance has no attribute '__test_2'
最后
以上就是香蕉水壶为你收集整理的python 类私有成员的全部内容,希望文章能够帮你解决python 类私有成员所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复