Python中单下划线和双下划线
2018-03-13 17:55
399 查看
>>> 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
相关文章推荐
- Python中单/双下划线使用
- Python中单下划线开头的特性
- Python中单/双下划线使用
- Python中单下划线和双下划线的区别
- Python中单下划线开头的特性
- python中单下划线_的常见用法总结
- Python中单下划线,双下划线,俩头下划线的区别。
- Python中单、双下划线的区别总结
- Python中单/双下划线使用 分类: python python基础学习 2013-09-02 21:31 1764人阅读 评论(0) 收藏
- Python中的魔术(双下划线'__xxx__')方法详解
- 详解Python中的下划线
- python下划线
- python中单,双引号和三个双引号的区别
- Python中单例设计模式
- Python中单引号,双引号,三引号的区别
- Python中下划线---完全解读
- Python单下划线、双下划线、头尾双下划线
- 【变量】关于python中的下划线
- python进阶12:单下划线与双下划线(私有和保护)
- Python中单引号,双引号,3个单引号及3个双引号的区别