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. android中LayoutInflater.from(context).inflate的分析

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  2. ES选主策略

    ES版本5.6.3 1.整个流程的开始,实在node启动后触发的,Node.java中start()方法,通过调用ZenDiscovery.java中的doStart()方法,之后会调用startIn ...

  3. New Concept English Two 14 34

    recently  busy  a lot ,just  practices   every   morning. $课文32  购物变得很方便 324. People are not so hone ...

  4. webpack 提升90%的构建速度 HardSourceWebpackPlugin

    HardSourceWebpackPlugin 插件 不能提升第一次构建的速度,但对于第二次构建能提升99%的构建速度 第一次构建: 第二次: 提升了..,算不出来,反正就是很多啦~~~ npm in ...

  5. 链表的实现、输出和反向 python

    链表节点包含两个元素:节点的值和指向的下一个节点,因此可以定义链表的类为: class linknode: def __init__(self,value=None,next=None): self. ...

  6. test20181019 B君的第三题

    题意 B 君的第三题(urumqi) 题目描述 风雨如晦,鸡鸣不已. B 君最近在研究自己的学长都在做什么工作,每个学长属于一个公司. B 君会获得一些信息,比如x 和y 在相同公司,x 和y 在不同 ...

  7. Javascript 全局函数是 window 的函数

    比如以下函数,看起来不属于任何对象,但它是一个全局对象. 它属于 HTML页面的函数. function myFunction(a, b){ return a * b; } window.myFunc ...

  8. 再看Spring Could微服务的关键组件

    Consul是用go开发的开源注册中心服务,内置服务发现与注册.raft一致性协议实现.健康检查.多数据中心等方案.与Eurker相比,consul还能对异构服务如rpc提供支持. 作为微服务系统的核 ...

  9. eclipse启动报错:An error has occurred.See the log file D:\eclipse\configuration\1552616709202.log

    如题,Eclipse崩了,只能按它留下的线索去看了1552616709202.log: !SESSION -- ::08.739 ----------------------------------- ...

  10. 租用游艇(简单区间dp)

    租用游艇 时间限制: 1 Sec  内存限制: 128 MB提交: 1  解决: 1[提交][状态][讨论版][命题人:quanxing] 题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2 ...