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. CentOS怎样安装Python3.6

    yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel安装可能用到的依赖 ...

  2. C语言——第四次作业(2)

    作业要求一 项目wordcount 设计思路:输入需统计的文件名,打开此文件,输入功能对应的字符,分别实现对应的功能,关闭文件. 主要代码 #include<stdio.h> #inclu ...

  3. jenkins执行shell命令,有时会提示“Command not found”

    这个问题其实就是环境变量没有配准确 (1)检查你在Jenkins中设置的maven是否准确,可以通过[new job]按钮查看新建job中是否有maven选项,没有就是你配置的不准确 如果你下载的插件 ...

  4. javaScript基础篇之数据类型

    我主要学习廖雪峰老师官方网站的javaScript,所以很多都是出自于廖老师,请见谅.以下是廖老师的官方网站的地址:http://www.liaoxuefeng.com/wiki/0014344466 ...

  5. centos7 开放mongodb端口

    CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启端口是不可以的 CentOS 7 采用了 firewalld 防火墙 如要查询是否开启27019端口则: 1 ...

  6. java之 JVM 内存管理详解

    一.JVM结构 根据<java虚拟机规范>规定,JVM的基本结构一般如下图所示: 从左图可知,JVM主要包括四个部分: 1.类加载器(ClassLoader):在JVM启动时或者在类运行时 ...

  7. VMware ESXi NAT实现

    VMware ESXi默认不支持NAT,但是我们如果只有一个外网端口映射,然后希望通过这个映射,从外网访问两台机器的话,那最好做NAT.这里我们通过一个开源的网络防火墙pfSense来实现NAT[1] ...

  8. Web service 框架比较CXF xfire aisx2 aisx

    Web 服务框架.它还体现了从 Axis 1.x 系列获得的经验和最近两年在 Web 服务领域的发展.推出 Axis2 的主要原因之一是从速度和内存方面获得更好的性能——不过还添加了一些新特性和功能. ...

  9. yii2 memcache 跨平台交互 键和值不一样

    1 首先在配置文件中加载 web\basic\config\web.php ........ 'components' => [ 'request' => [ // !!! insert ...

  10. 让多个Fragment 切换时不重新实例化

    转自:http://www.yrom.net/blog/2013/03/10/fragment-switch-not-restart/ 让多个Fragment 切换时不重新实例化 在项目中需要进行Fr ...