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. bzoj2631

    题解: lct+链上修改 每一次修改的时候记录lazy标记 如果有了乘法,加法的lazy标记也要相应的随之变化 代码: #pragma GCC optimize(2) #include<bits ...

  2. 关于VC中LineDDA函数的调用

    在项目里碰到这个函数,不知道怎么使用,记录在这里. 该函数的原型如下: BOOL LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd, LIN ...

  3. 活字格企业Web应用生成器荣获"2017年度优秀软件产品"

    日前,中国软件行业协会权威发布"2017年度优秀软件产品"名单,活字格企业 Web 应用生成器凭借产品的独特优势,和近年来在企业Web应用方面的杰出贡献,位列其中,受到用户和行业专 ...

  4. 走在linux 的路上

    终于现在不看鸟哥的私房菜基础篇了,以后再慢慢看,像我这种初学者,感觉还是不太适合看鸟哥的私房菜. 于是从图书馆借了本书继续学习我的linux. 这样看着linux容易多了,进而熟悉了几个命令:ls c ...

  5. linux下文件解压

    这几天要学THINKPHP框架了,于是从网上下载了一个压缩包,后缀是.zip的,解压方法为 unzip ###.zip 随便把linux下的.rar方法也写下来,这两个都不常见. unrar    e ...

  6. 20155318 2016-2017-2 《Java程序设计》第八学习总结

    20155318 2016-2017-2 <Java程序设计>第八学习总结 教材学习内容总结 学习目标 了解NIO 会使用Channel.Buffer与NIO2 会使用日志API.国际化 ...

  7. Codeforces 165 E. Compatible Numbers【子集前缀和】

    LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...

  8. ssh的时候提示No user exists for uid 501

    原来是oh my zash升级导致的.关闭iterm2然后重新打开iterm2就可以了

  9. rabbitMQ应用,laravel生产广播消息,springboot消费消息

    最近做一个新需求,用户发布了动态,前台需要查询,为了用户读取信息响应速度更快(MySQL很难实现或者说实现起来很慢),所以在用户动态发布成功后,利用消息机制异步构建 redis缓存 和 elastic ...

  10. linux Xinetd服务简介

    http://www.chuanke.com/course/72351180839190528______.html 1.什么是xinetdextended internet daemonxinetd ...