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. 改造 Ace Admin 模板的 ace_tree 组件的 folderSelect 样式

    *注:我用的Ace Admin版本为1.3.4 Ace Admin 是一个轻量,功能丰富,HTML5.响应式.支持手机及平板电脑上浏览的优秀管理后台模板. 关于tree的使用,html文件夹下tree ...

  2. web 常用富文本编辑器

    1. 百度家的 UEditor  官网地址http://ueditor.baidu.com/website/;在线演示地址:http://ueditor.baidu.com/website/onlin ...

  3. 第13课:HTML基础之DOM操作2

    1. 1)d.innerHTML:标签中的所有内容 删除某个标签 a)可以直接修改innerHTML的值: b) 2)d.innerText:标签中的文本内容 3)input.value='aaa'  ...

  4. New Concept English Two 8 19

    pls  practice every day $课文17 青春常驻 161. My aunt Jennifer is an actress. 我的姑姑詹妮弗是位演员, 162. She must b ...

  5. layers框架

  6. 常见HTTP状态码(200、301、302、500等) 释义

    对网站管理工作者来说有个词不陌生,HTTP状态码,它是用以表示网页服务器HTTP响应状态的3位数字代码.状态码的第一个数字代表了响应的五种状态之一. 1XX系列:指定客户端应相应的某些动作,代表请求已 ...

  7. c++ 声明和定义的区别

    从编译原理上来说,声明是仅仅告诉编译器,有个某类型的变量会被使用,但是编译器并不会为它分配任何内存.而定义就是分配了内存. int a;在外面是作为一个语句,这就是定义,会构造对象,定义本身也是声明. ...

  8. 让APK 成功在 Windows 运行并可以设置本地文件

    让APK 成功在 Windows 运行并可以设置本地文件 安装 ARC Welder. 启动 ARC Welder 后选反apk 文件,下载 zip. 将 zip 解压修改 manifest.json ...

  9. Tomcat 7 的七大新特性(更容易将Tomcat内嵌到应用去中去 )

    Tomcat的7引入了许多新功能,并对现有功能进行了增强.很多文章列出了Tomcat 7的新功能,但大多数并没有详细解释它们,或指出它们的不足,或提供代码示例.本文将明确描述TOMCAT 7中七个最显 ...

  10. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...