默认情况下,属性在Python 中都是“public”。

1:双下划线(__)

Python 为类元素(属性和方法)的私有性提供初步的形式。由双下划线开始的属性在运行时被“混淆”,所以直接访问是不允许的。实际上,会在名字前面加上下划线和类名。

class pubpri(object):
def __init__(self, num):
self._num= num
self.__num = num
self.__num__= num
self.pubnum = num >>> n1 = pubpri(3) >>> n1._num
3 >>> n1.__num__
3 >>> n1.pubnum
3 >>>n1.__num
Traceback(most recent call last):
File"<stdin>", line 1, in <module>
AttributeError:'pubpri' object has no attribute '__num' >>>n1._pubpri__num
3

尽管这样做提供了某种层次上的私有化,但算法处于公共域中,并且很容易被破解。

这种名字混淆的另一个目的,是为了保护__XXX 变量不与父类名字空间相冲突。如果在类中有一个__XXX 属性,它将不会被其子类中的__XXX 属性覆盖。使用__XXX,子类的代码就可以安全地使用__XXX,而不必担心它会影响到父类中的__XXX。

2:单下划线(_)

在一个模块中以单下划线开头的变量和函数被默认当作内部函数,如果使用from a_module import * 导入时,这部分变量和函数不会被导入。不过值得注意的是,如果使用 import a_module 这样导入模块,仍然可以用a_module._some_var 这样的形式访问到这样的对象。比如,假设test.py内容如下:

import  sys
_modulenum = 3
__modulenum = 3
__modulenum__ = 3
pubnum = 3 def _fun():
print 'this is',sys._getframe().f_code.co_name def __fun():
print'this is ', sys._getframe().f_code.co_name def __fun__():
print'this is ', sys._getframe().f_code.co_name def fun():
print'this is ', sys._getframe().f_code.co_name >>> from test import *
>>>_modulenum
...
NameError: name'_modulenum' is not defined >>>__modulenum
...
NameError: name'__modulenum' is not defined >>>__modulenum__
...
NameError: name'__modulenum__' is not defined >>>pubnum
3 >>> _fun()
...
NameError: name'_fun' is not defined >>> __fun()
...
NameError: name'__fun' is not defined >>>__fun__()
...
NameError: name'__fun__' is not defined >>> fun()
this is fun >>> import test
>>>test._modulenum
3 >>>test.__modulenum
3 >>>test.__modulenum__
3 >>>test.pubnum
3 >>>test._fun()
this is _fun >>>test.__fun()
this is __fun >>>test.__fun__()
this is __fun__ >>>test.fun()
this is fun

双下划线开头双下划线结尾的是一些 Python 的特殊对象,如类成员的 __init__、__del__、__add__、__getitem__等。 Python 官方推荐永远不要将这样的命名方式应用于自己的变量或函数,而是按照文档说明来使用。

注意,私有化都是针对外部而言,在类内部,依然可以使用正常的访问方式,在类的外部就必须进行“混淆”了。比如:

class test(object):
__cversion =1.1 def __init__(self):
self.__iversion = 1.2 def fun(self):
print self.__iversion
print test.__cversion
print self._test__iversion
print self._test__cversion >>> from test import test
>>> t1 = test()
>>> t1.fun()
1.2
1.1
1.2
1.1 >>> t1.__iversion
Traceback (most recent call last):
File"<stdin>", line 1, in <module>
AttributeError: 'test' object has no attribute'__iversion' >>> t1._test__iversion
1.2 >>> t1.__cversion
Traceback (most recent call last):
File"<stdin>", line 1, in <module>
AttributeError: 'test' object has no attribute'__cversion' >>> t1._test__cversion
1.1

参考:

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

Python基础:15私有化的更多相关文章

  1. 十五. Python基础(15)--内置函数-1

    十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...

  2. python基础15下_迭代器_生成器

    print(dir([])) #告诉我列表拥有的所有方法 # 双下方法 # print([1].__add__([2])) print([1]+[2]) ret = set(dir([]))& ...

  3. python基础(15):内置函数(一)

    1. 内置函数 什么是内置函数? 就是python给你提供的,拿来直接⽤的函数,比如print,input等等,截⽌到python版本3.6.2 python⼀共提供了68个内置函数.他们就是pyth ...

  4. python基础15上_迭代器_生成器

    # 迭代器和生成器 # 迭代器: # 双下方法 : 很少直接调用的方法.一般情况下,是通过其他语法触发的 # 可迭代的 —— 可迭代协议 含有__iter__的方法('__iter__' in dir ...

  5. 『Python基础-15』递归函数 Recursion Function

    什么是递归函数 一种计算过程,如果其中每一步都要用到前一步或前几步的结果,称为递归的.用递归过程定义的函数,称为递归函数,例如连加.连乘及阶乘等.凡是递归的函数,都是可计算的,即能行的. 递归就是一个 ...

  6. 2015/9/19 Python基础(15):变量作用域及生成器

    变量作用域标识符的作用域是定义为其声明的可应用范围,或者即是我们所说的变量可见性.也就是,我们可以在程序的那个部分去访问一个制定的标识符.全局变量与局部变量定义在函数内的变量有局部作用域,在一个模块中 ...

  7. python基础15 ---面像对象的程序设计

    面向对象的程序设计 一.面向对象的程序设计简介 1.面向对象程序设计的由来. 我们之前虽然学习过了面向过程的程序,它的核心是面向过程,一步一步的设计好了的流程,虽然极大的降低了程序的复杂度,但是一个设 ...

  8. python基础——15(加密、excel操作、ini文件操作、xml操作模块及数据格式分类)

    一.加密模块 1.有解密的加密方式(base64) #base64加密 import base64 str_encrypt = input("输入要加密的字符串:\n") base ...

  9. Day15 - Python基础15 模块学习-selectors

    本节内容 1:Python/selectors模块 2:selsect实例 1:Python/selectors模块及队列  selectors模块是可以实现IO多路复用机制: 它具有根据平台选出最佳 ...

  10. 『无为则无心』Python基础 — 15、Python流程控制语句(for循环语句)

    目录 1.for循环语法 2.for循环中的break和continue 3.循环+else结构 (1)while...else (2)while...else退出循环的方式 (3)for...els ...

随机推荐

  1. nginx、php-fpm启动脚本

    Nginx官方启动脚本 //service nginx stop|start|restart|reloadtouch /etc/init.d/nginx chmod nginxvi /etc/init ...

  2. linux把普通用户添加到sudo组

    一.linux下把普通用户添加到sudo组的方式: 1. root权限下, 先cd到/etc目录下 2. 由于sudoers文件为只读权限,所以需要添加写入权限,chmod u+w sudoers 3 ...

  3. python实例 输出你好

    #打开新窗口,输入: #! /usr/bin/python # -*- coding: utf8 -*- s1=input("Input your name:") print(&q ...

  4. leetcode 60-80 easy

    66.Plus One Given a non-empty array of digits representing a non-negative integer, plus one to the i ...

  5. 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序

    题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...

  6. 多线程 多进程 协程 Queue(爬虫代码)

    快速理解多进程与多线程以及协程的使用场合和特点 首先我们来了解下python中的进程,线程以及协程! 从计算机硬件角度: 计算机的核心是CPU,承担了所有的计算任务.一个CPU,在一个时间切片里只能运 ...

  7. Web渗透三字经

    网络上曾流传一段Web渗透三字经,如下: 用搜索 找注入 没注入 就旁注 没旁注 用0day 没0day 扫目录 找后台 爆账户 传小马 放大马 拿权限 挂页面 放暗链 清数据 清日志 留后门 然后我 ...

  8. pytest fixture 利用 params参数实现用例集合

    @pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...

  9. linux 添加文字、图形、线条、箭头的 截图

    1.deepin-screenshot 截图 软件包里搜索deepin-screenshot 支持添加文字.图形.线条.箭头的功能 2.字体发虚 Linux mint 开始菜单等字体不清晰 在软件管理 ...

  10. qt获取屏幕

    m_pDesktopWidget = QApplication::desktop(); // 屏体数量,包含扩展屏 int screenCount = m_pDesktopWidget->scr ...