• python无私有成员变量


    python解释器将__init__函数里的__z变量转成 _classname__z了。明确规则后外部依旧能够通过实力对象来訪问。

    In [1]: class aa:
       ...:     def __init__(self):
       ...:         self.x = 10
       ...:         self.y = 11
       ...:         self.__z = 12
       ...:     
    
    In [2]: a = aa()
    
    In [3]: print a.x
    10
    
    In [4]: print a.y
    11
    
    In [5]: print a.__z
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-5-a90abb2fd47d> in <module>()
    ----> 1 print a.__z
    
    AttributeError: aa instance has no attribute '__z'
    
    In [6]: dir(a)
    Out[6]: ['__doc__', '__init__', '__module__', '_aa__z', 'x', 'y']
    
    In [7]: print _aa.__z
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-7-b2d0b9a63937> in <module>()
    ----> 1 print _aa.__z
    
    NameError: name '_aa' is not defined
    
    In [8]: print _aa__z
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-8-80bb4dde0a55> in <module>()
    ----> 1 print _aa__z
    
    NameError: name '_aa__z' is not defined
    
    In [9]: print a._aa__z
    12
    
    In [10]: a.__z = 14
    
    In [11]: dir(a)
    Out[11]: ['__doc__', '__init__', '__module__', '__z', '_aa__z', 'x', 'y']
    
    In [12]: print a._aa__z
    12
    





  • 相关阅读:
    Voiceover “眼里” 的HTML5标签
    Edge浏览器默认地址被百度劫持
    How to fix "ReferenceError: primordials is not defined" error
    php 读文件
    jsconfig.json: Unexpected token ] in JSON at position
    公司入域电脑更新遇到 0x8024401c 解决办法
    颜色计算
    Terminal 美化
    highcharts 查看配置
    cra
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7106422.html
Copyright © 2020-2023  润新知