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. pos 和 AnsiPos

    PropsClearList[I]的值是 用户=个人 R := AnsiPos(Equal_sign, PropsClearList[I]); ShowMessage( IntToStr( R));/ ...

  2. 让IE兼容console——“由于出现错误80020101而导致此项操作无法完成”的解决方案

    问题描述 经测试发现问题只出现在: 1.原生IE8(其他版本IE模拟出的IE8无此问题) 2.从打开IE8没有开启过F12(曾经开启过又关闭的无此问题) IE8报错“由于出现错误80020101而导致 ...

  3. SSRS实现 矩阵列实现动态列

    需求背景:按区域查询店铺的销售量和库存量,然后店铺的数据是动态的.实现类似效果如下图,其中蓝色框是动态的,店铺的数量是不确定的,可能是3个,也可能是20个. 步骤一: 步骤二: 加工后: 最后效果:

  4. ssh命令的使用

    ssh命令是用来远程登录服务器的,默认端口号为22. 常用的命令是: ssh   用户名@服务器地址 然后回车,输入登录服务器的密码 登录服务器后要进行上传下载文件,可以用scp命令,命令格式为: s ...

  5. UOJ117. 欧拉回路【欧拉回路模板题】

    LINK 题目大意 就是让你对有向图和无向图分别求欧拉回路 非常的模板,但是由于UOJ上毒瘤群众太多了 所以你必须加上一个小优化 就是每次访问过一个边就把它删掉 有点像Dinic的当前弧优化的感觉 注 ...

  6. 【传输协议】HttpClient基本使用

    最近工作中是做了一个handoop的hdfs系统的文件浏览器的功能,是利用webhdfs提供的rest api来访问hdfs来与hdfs进行交互的,其中大量使用HttpClient,之前一直很忙,没什 ...

  7. 【thrift】thrift入门初探--thrift安装及java入门实例

    转载:https://www.cnblogs.com/fingerboy/p/6424248.html 公司的一些平台服务框架底层封装了thrift提供服务,最近项目不是很紧,于是研究了一下,刚刚入门 ...

  8. test20181020 B君的第二题

    题意 分析 考场70分 一看就是裸的kmp,直接打上去. #include<cstdlib> #include<cstdio> #include<cmath> #i ...

  9. 结构化日志类库 ---- Serilog库

    在过去的几年中,结构化日志已经大受欢迎.而Serilog是 .NET 中最著名的结构化日志类库 ,我们提供了这份的精简指南来帮助你快速了解并运用它. 0. 内容 设定目标 认识Serilog 事件和级 ...

  10. 图形学习 Javascript 正则 regexper.com

    regexper.com 可以很方便的显示出正则图示,方便学习正则. 比如正则 ^([a-zA-Z0-9+_-])+@([a-zA-Z_-])+(\.[a-zA-Z0-9_-])+ 一目了然,直观显示 ...