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. 2018.12.21 Cmos- RF

    1 PSK 趋肤效应 50欧匹配 smith s参数 :在datasheet里面会有 Information coding Bluetooth : Continous Variable Slope D ...

  2. Python源码分析之dis

    一.简单例子 def add(a, b): return a + b add_nums.py import foo a = [1, 'python'] a = 'a string' def func( ...

  3. HDU3530 Subsequence(单调队列)

    题意是说给出一个序列,现在要求出这个序列的一个最长子区间,要求子区间的最大值与最小值的差在[m, k]范围内,求区间长度 做法是维护两个队列,一个维护到当前位置的最大值,一个维护最小值,然后计算当前节 ...

  4. linux sort 多列正排序,倒排序

    转载:https://segmentfault.com/a/1190000005713784 sort是在Linux里非常常用的一个命令,管排序 sort将文件的每一行作为一个单位,相互比较,比较原则 ...

  5. JFrog Artifactory CE c&&c++ 包管理工具

    JFrog Artifactory CE 支持conan 以及普通二进制c&&c++包管理 使用docker 进行环境的搭建测试 安装 docker run -d -p 8081:80 ...

  6. 移植SDL最新版本(转)

    原文出自:http://blog.csdn.net/flyyang123456789/article/details/17223485 首先 将所要移植的包准备好  有 SDL2-2.0.1.tar. ...

  7. swing版网络爬虫-丑牛迷你采集器2.0

    swing版网络爬虫-丑牛迷你采集器2.0 http://www.javacoo.com/code/704.jhtml 整合JEECMS http://bbs.jeecms.com/fabu/3186 ...

  8. Oracle VM VirtualBox虚拟机安装Ubuntu Server

    安装过程如下:原文转自:http://www.linuxidc.com/Linux/2012-04/59368p8.htm

  9. MTU&MSS

    MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...

  10. 北京师范大学第十六届程序设计竞赛决赛-重现赛-B题

    一.题目链接 https://www.nowcoder.com/acm/contest/117/B 二.题意 给定一组序列$a_1,a_2,\cdots,a_n$,表示初始序列$b_1,b_2,\cd ...