python单/双下划线使用
在Python编程中经常会遇到函数(function),方法(method)及属性(attribute)以下划线'_'作为前缀,这里做个总结。
1. object # public
2. __object__ # special, python system use, user should not define like it
3. __object # private (name mangling during runtime)
4. _object # obey python coding convention, consider it as private
class Foo():
def __init__():
...
def public_method():
print 'This is public method'
def __fullprivate_method():
print 'This is double underscore leading method'
def _halfprivate_method():
print 'This is one underscore leading method' f = Foo()
f.public_method() # OK
f.__fullprivate_method() # Error occur
f._halfprivate_method() # OK
上文已经说明了,python中并没有真正意义的private,见以下方法便能够访问__fullprivate_method()
python单/双下划线使用的更多相关文章
- python面向对象双下划线方法与元类
目录 双下划线方法(__) 元类简介 产生类的两种表现形式 元类的基本使用 元类进阶操作 __new__方法 双下划线方法(__) 面向对象中的双下方法也有一些人称之为是魔法方法,有些双下方法不需要刻 ...
- 理解Python的双下划线命名(转)
add by zhj:今天在学习SimpleHTTPServer的源代码时,看到了Python标准库SocketServer模块中有个BaseServer类,该类的__init__方法定义如下 def ...
- 理解Python的双下划线命名
引子 我热情地邀请大家猜测下面这段程序的输出: class A(object): def __init__(self): self.__private() ...
- python 类 双下划线解析
__getattr__用法:说明:这是python里的一个内建函数,当调用的属性或者方法不存在时,该方法会被调用调用不存在的属性调用不存在的方法
- python中单下划线和双下划线的区别
1.python中双下划线(__str__)代表这个变量是特殊变量,是可以直接访问的 __xxx___ 定义的是特列方法.像__init__之类的 2.python前面双划线(__name)代表这个变 ...
- Python中单下划线与双下划线用法总结
看mentor的脚本时,遇到self._item.callspec.getparam('')语句,理解起来比较困难,找到一篇文章,记录的比较详细,特别记录一下,以备复习. 附链接地址:http://w ...
- python——双下划线与python命名机制
python中双下划线的作用(1)所有以双下划线开头的成员是私有的(2)python对于私有变量是会进行扎压(mangling)的,扎压规则是原始定义:class A(): __function ...
- Python中的下划线(译文)
原文地址这篇文章讨论Python中下划线_的使用.跟Python中很多用法类似,下划线_的不同用法绝大部分(不全是)都是一种惯例约定. 单个下划线(_) 主要有三种情况: 1. 解释器中 _符号是指交 ...
- 详解 Python 中的下划线命名规则
在 python 中,下划线命名规则往往令初学者相当 疑惑:单下划线.双下划线.双下划线还分前后……那它们的作用与使用场景 到底有何区别呢?今天 就来聊聊这个话题. 1.单下划线(_) 通常情况下,单 ...
随机推荐
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
寻找最近公共祖先,示例如下: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ ...
- POJ 3522 Slim Span
题目链接http://poj.org/problem?id=3522 kruskal+并查集,注意特殊情况比如1,0 .0,1.1,1 #include<cstdio> #include& ...
- 【LeetCode练习题】Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- POJ3307+找规律
/* 题意:求第N个productivity property数是谁. (productivity property数:就是这个数可以由另外的数的各个位上的乘积得到.) */ #include< ...
- A. Anton and Letters
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 启动监听报错:TNS-12537: TNS:connection closed TNS-12560: TNS:protocol adapter error TNS-00507: Connection closed Linux Error: 29: Illegal seek
启动监听程序报错: 说明:在rhel5.8上安装完成oracle11g数据库后,使用netca创建完监听,启动监听时报错.还未使用dbca创建实例. [oracle@rusky-oracle11g ~ ...
- UVA 10305 Ordering Tasks
题意: 给出n和m,n代表总共有几个箱子.接下来m行,每行有a,b,表示b在a之后.输出一个合理的序列. 分析: 简单的拓扑排序: 代码: #include <iostream>#incl ...
- RadioButton、CheckBox与ToggleButton
1.RadioButton RadioButton被称作为单选框,通常都是以组的形式出现,可以在一组控件中选择一个. RadioButton的使用首先需要加入<RadioGroup/>,在 ...
- NPOI导出为Excel文件
1.添加引用 2.将ExcelRender.cs和SqlHelper.cs两个类拷贝到App_Code文件夹下 3.写后台代码 eg:根据部门和日期导出成绩表 /// <summary> ...
- javascript 压缩空格代码演示
压缩空格代码演示 主要是讲解 压缩一个字符串两段空格 例如:javascript函数里的空格不论是这样 var s = "Hello World ...