Python中定义“私有”成员变量和成员函数

在学习Python的过程中发下,它把类(class)中所有的成员函数和成员变量都看做是"Public"的,作为C++出身的程序员们可能就不习惯了。

Python的官方教程中如是说:““Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.”。也就是说,在Python中我们不能够像C++或者Java那样有专门的private和public关键字来指定某些成员是公有的,哪些成员是私有的。

然而,Python教程中又说了:“However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.”。

OK,原来在Python中大家都是通过在一个变量或者函数之前加上下划线来表示私有变量的,例如__spam(这里是两个下划线)就是私有的。

同时,Python为了保证不能再class之外访问该变量,“Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.”,意思就是说,Python会在类的内部自动的把你定义的__spam变量的名字替换成为 _classname__spam(注意,classname前面是一个下划线,spam前是两个下划线),Python把这种技术叫做“name mangling”。因此,用户在外部访问__spam的时候就会提示找不到相应的变量。

下面给出一段简单的代码:


[python]  view plain  copy
  1. #!/usr/bin/python  
  2. #Filename: classPrivate.py  
  3.   
  4. class testPrivate:  
  5.     def __init__(self):  
  6.         self.__data = []  
  7.   
  8.     def add(self,item):  
  9.         self.__data.append(item)  
  10.     def printData(self):  
  11.         print (self.__data)  
  12.   
  13. t = testPrivate()  
  14. t.add('dancingrain')  
  15. t.add('hello')  
  16. t.printData()  
  17. print(t.__data)  

输出结果是:



可以看出,当执行print(t.__data)的时候提示我们没有该属性,OK!这就是我们想要的结果。


但是,这并不是意味着我们真的就不能够从外部访问这个变量了,上面说Python在类的内部用_classname__spam替换了__spam,将数据的属性用:__属性名,替换为,_类名__属性名

因此,我们可以在类的外面使用_classname__spam来访问__spam。看看下面的代码:


[python]  view plain  copy
  1. #!/usr/bin/python  
  2. #Filename: classPrivate.py  
  3.   
  4. class testPrivate:  
  5.     def __init__(self):  
  6.         self.__data = []  
  7.   
  8.     def add(self,item):  
  9.         self.__data.append(item)  
  10.     def printData(self):  
  11.         print (self.__data)  
  12.   
  13. t = testPrivate()  
  14. t.add('dancingrain')  
  15. t.add('hello')  
  16. t.printData()  
  17. print(t._testPrivate__data)  

结果是:




Python中单下划线和双下划线

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

__foo__:一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如__init__(),__del__(),__call__()这些特殊方法

_foo:一种约定,用来指定变量私有.程序员用来指定私有变量的一种方式.不能用from module import * 导入,其他方面和公有一样访问;

__foo:这个有真正的意义:解析器用_classname__foo来代替这个名字,以区别和其他类相同的命名,它无法直接像公有成员一样随便访问,通过对象名._类名__xxx这样的方式可以访问.

详情见:http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python

或者: http://www.zhihu.com/question/19754941



参考:

http://blog.csdn.net/doufei_ccst/article/details/9152085

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值