python之private variables

  “Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

  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:

  

[demo]

 class Point:
def __init__(self):
self.__count = 10;
def __show(self):
print 'Hello world' pt = Point()
print pt._Point__count
pt._Point__show()

上例可以看到, 直接调用 pt.__count || pt.__show() 会发现找不到属性, 必须加上 _Point前缀才行.

python之private variables的更多相关文章

  1. OOP in JS Public/Private Variables and Methods

    Summary private variables are declared with the 'var' keyword inside the object, and can only be acc ...

  2. python之private variable

    [python之private variable] Since there is a valid use-case for class-private members (namely to avoid ...

  3. 笨办法学Python - 习题4: Variables and Names

    1.习题 4: 变量(variable)和命名 学习目标:了解Python中变量的定义,学习定义简明易记的变量名 变量:变量是存储内存中的值,就是每定义一个变量就会在内存中开辟一个空间.基于变量的类型 ...

  4. [Javascript] Private Variables with IIFEs

    An IIFE (immediately invoked function expression) is when a function is called immediately after it ...

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

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

  6. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

  7. matlab vs python

    (参考)从下图可以清晰看到matlab和python之间的区别 Python是一种编程语言,但与其他变成语言的不同在于:python具有许多的扩展库(通过import引入) Matlab是集合计算环境 ...

  8. Python Tutorial 学习(九)--Classes

    ## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...

  9. Python Tutorial 学习(六)--Modules

    6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...

随机推荐

  1. LINUX中的RCU机制的分析

    RCU机制是Linux2.6之后提供的一种数据一致性访问的机制,从RCU(read-copy-update)的名称上看,我们就能对他的实现机制有一个大概的了解,在修改数据的时候,首先需要读取数据,然后 ...

  2. 修改Intelij IDEA的maven依据下载为国内镜像(阿里)

    1.win7环境,默认情况下在用户目录的.m2下自己新建setting文件.QQ群交流:697028234 .m2\settings.xml 2.settings.xml文件内容为: <sett ...

  3. 免费获取半年 Bitdefender Total Security 2014

    免费获取半年 Bitdefender Total Security 2014,安装后剩余 200 天使用期.安装程序语言是德语,调包下安装语言应该也是可以的? 目前德国活动,Bitdefender T ...

  4. 『转』G Data InternetSecurity 2014 – 免费3个月

    G Data来自德国的顶级杀毒软件,采用BitDefender+CloseGap双引擎,屡获AV-TEST防護率100%.不多介绍,目前2014中文版没有上市.活动地址:点此进入官方网站:点此进入申请 ...

  5. Win7安装netbeans 找不到JDK

    安装netbeans,直接运行官方下载的.exe安装包,提示找不到jdk,我jdk已安装1.7,环境变量已配好.然后百度,找到很多都是linux下的解决方案,win7的很少,然后看到说--javaho ...

  6. 實驗項目wordcount

    wordcount 1.设计思路 第一步 :主函数参数使用命令行参数,定义一个文件指针fp. 第二步:判断能否用只读的形式打开命令行指针中的文件,并让指针指向打开函数,若不能则输出不能读取文件,否则下 ...

  7. 对MYSQL慢查询slow query 日志记录内容的疑惑

    初始:由于新装服务器出现CPU占用过高,响应不及时的问题排查,因为环境基于最基础的LAMP构架 想到开启 MYSQL slow_query_log 慢查询日志做原因分析: 但是看到日志内容之后有点茫然 ...

  8. numpy pandas matplotlib

    import numpy as np import pandas as pd import matplotlib.pyplot as plt ---------------numpy--------- ...

  9. centos7 开放mongodb端口

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

  10. 打造html右键菜单

    今天是给大家介绍一款在网页上使用的右键菜单,原作者的网址是:http://51jsr.javaeye.com/blog/305517 这个右键菜单已经非常优秀,不过呢.却是IE Only,而且在DTD ...