python之private variable

  Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. 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. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls. For example:

class Mapping:
def __init__(self, iterable):
self.items_list = []
self.__update(iterable) def update(self, iterable):
for item in iterable:
self.items_list.append(item) __update = update # private copy of original update() method class MappingSubclass(Mapping): def update(self, keys, values):
# provides new signature for update()
# but does not break __init__()
for item in zip(keys, values):
self.items_list.append(item)

参考:http://docs.python.org/2.7/tutorial/classes.html#multiple-inheritance

python之private variable的更多相关文章

  1. Private Variable and Private Method - Python 私有变量 和 私有方法

    Private Variable and Private Method Python 不象 Java 那样, 通过 private 关键字创建私有属性, python 通过更简洁的实现了'私有属性', ...

  2. Python私有变量(Private Variable)

    Variables can be private which can be useful on many occasions. A private variable can only be chang ...

  3. python之private variables

    [python之private variables] “Private” instance variables that cannot be accessed except from inside a ...

  4. Python 變量 Variable 動態綁定

    為何 Python 變量沒有 Data Type 概念 ? 可以與任意 Data Type 綁定? Python 變量 Variable 與其他程式語言不同之處在於: > variable 不是 ...

  5. Private Variable

    Any variable defined inside a function is considered private since it is inaccessable outside that f ...

  6. To add private variable to this Javascript literal object

    You can use number as function/variable name, the numberic name can't be accessed from parent scope, ...

  7. python: local variable 'xxx' referenced before assignment

    问题发现 xxx = 23 def PrintFileName(strFileName): if xxx == 23: print strFileName xxx = 24 PrintFileName ...

  8. python:UnboundLocalError: local variable 'xxx' referenced before assignment

    近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...

  9. python 从private key pem文件中加载public key

    import rsa import logging from Crypto.PublicKey import RSA class RsaUtil: def __init__(self, pem_fil ...

随机推荐

  1. bzoj2759

    题解: lct+解线性方程组 首先先把每一个环搞出来,然后再建立一个额外的点 然后解方程.. 代码: #include <bits/stdc++.h> using namespace st ...

  2. PLSQL 触发器

    触发器权限 数据库创建用户时想要在本用户下使用触发器,需要给用户触发器的权限 使用DBA用户执行  GRANT CREATE TRIGGER TO user_name; 如果想在当前用户下创建其他用户 ...

  3. JS使用及技巧.

    JS小技巧 1.如果你JS了解的还不深请看 汤姆大叔的博客 ,肯定让你上升一个台阶. 2.百科全书 MDN. 3.两个非常简单实用的提示插件 toastr sweetalert. 4.数据类型的复制: ...

  4. -webkit新属性 image-set和srcset

    响应式图片的作用: 为使用不同分辨率的不同浏览器用户提供适合其浏览环境的图片大小的解决方案. 之前的解决方法是使用@media 但是-webkit新提出的image-set和srcset同样可以解决问 ...

  5. java基础第9天

    抽象 abstract 抽象类和抽象方法必须用abstract关键字修饰 抽象类格式 abstract class 类名{} 抽象方法定义,在返回值钱,或修饰符前加上abstract关键字 方法没有方 ...

  6. myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为????

    B 问题:myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为???? 解决:调整JSP页面编码:gb2312--->utf-8

  7. Linux运维学习笔记-定时任务知识总结

    定时任务编辑规范流程: 重要知识点: 切记用全路径编写定时脚本.定时任务 大部分在 crontab 计划任务中都会年到未尾带 >/dev/null 2>&1,是什么意思呢? > ...

  8. 爸妈才是最好的避孕药--------"北大状元拉黑父母事件的一些感想"

    今天看了这么一篇文章,地址:  http://mini.eastday.com/mobile/180131180318786.html <北大状元拉黑父母6年:你敢恨爸妈,可你敢原谅他们吗?&g ...

  9. minicom 十六进制(hex)显示接收数据

    /******************************************************************************** * minicom 十六进制(hex ...

  10. Linux 中同名进程的查杀

    长久一段时间没有做任何工作总结了,如果用工作忙来敷衍那是欺骗自己,承认这一段时间拒绝进步了. 在系统运维中,有许多同名进程需要kill是常有的事情, 数一下battle这个进程的数量 [root@HD ...