python之private variables
[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的更多相关文章
- 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 ...
- python之private variable
[python之private variable] Since there is a valid use-case for class-private members (namely to avoid ...
- 笨办法学Python - 习题4: Variables and Names
1.习题 4: 变量(variable)和命名 学习目标:了解Python中变量的定义,学习定义简明易记的变量名 变量:变量是存储内存中的值,就是每定义一个变量就会在内存中开辟一个空间.基于变量的类型 ...
- [Javascript] Private Variables with IIFEs
An IIFE (immediately invoked function expression) is when a function is called immediately after it ...
- python 从private key pem文件中加载public key
import rsa import logging from Crypto.PublicKey import RSA class RsaUtil: def __init__(self, pem_fil ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
- matlab vs python
(参考)从下图可以清晰看到matlab和python之间的区别 Python是一种编程语言,但与其他变成语言的不同在于:python具有许多的扩展库(通过import引入) Matlab是集合计算环境 ...
- Python Tutorial 学习(九)--Classes
## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...
- Python Tutorial 学习(六)--Modules
6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知 ...
随机推荐
- 看我如何快速学习.Net(高可用数据采集平台)
最近文章:高可用数据采集平台(如何玩转3门语言php+.net+aauto).高并发数据采集的架构应用(Redis的应用) 项目文档:关键词匹配项目深入研究(二)- 分表思想的引入 吐槽:本人也是非常 ...
- springboot搭建的2种方式
一.搭建springboot项目有两种方式1.继承springboot项目 <parent> <groupId>org.springframework.boot</gro ...
- hashcode()和equals()方法
(一)hashcode(): 当Set接收一个元素时根据该对象的内存地址算出hashCode,看它属于哪一个区间,再这个区间里调用equeals方法.这里需要注意的是:当俩个对象的hashCode值相 ...
- REST风格
1)Representational State Transfer,表述性状态转移,是一种软件架构风格 2)实现步骤 第一步:修改URL 例:http://localhost:8090/SMBMS_C ...
- mongdb分布式
搭建高可用mongodb集群(一)——配置mongodb Posted on 17 十一月, 2013 by lanceyan | 21 Replies 在大数据的时代,传统的关系型数据库要能更高的服 ...
- (转)OAuth 2.0的设计思路
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- Servlet不是线程安全的。
要解释为什么Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP请求时,Tomcat从线程池中取出一个 ...
- cocos2dx切换场景
第一屏必须: auto scene = GameMenu::createScene(); director->runWithScene(scene); 然后是主场景: auto scene = ...
- 深入理解Java虚拟机,gc输出参数
https://blog.csdn.net/qq_21383435/article/details/80702205
- oracle 表空间总结
表空间总结 一.认识表空间 1:表空间概念: 表空间是数据库中最大的逻辑单位,Oracle数据库采用表空间将相关的逻辑组件组合在一起,一个Oracle数 ...