class a:
def __init__(self):
self._b()#why here use _b,not b,What's the difference
self._c='cccc'#why here use _c,not c,What's the difference
def _b():
print 'bbbb'
Run Code Online (Sandbox Code Playgroud)
a.py
class a:
def __init__(self):
self._b()#why here use _b,not b,What's the difference
self._c='cccc'#why here use _c,not c,What's the difference
def _b(self):
print 'bbbb'
Run Code Online (Sandbox Code Playgroud)
b.py
from a import *
b=a()
b._b()
print b._c
Run Code Online (Sandbox Code Playgroud)
它打印bbbb bbbb bbbb bbbb cccc
为什么可以打印出这些,不是_b和_c私有变量.
根据pep8(http://www.python.org/dev/peps/pep-0008/),单个下划线旨在表示"通常为私有"的方法或属性.解释器本身没有与单个下划线的交互,它更像是一个约定.双下划线OTOH在翻译中具有重要意义.
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |