返回信息流对python变量的access scope不是很理解,今天一不小心就弄出个问题
#!/usr/bin/python
__UPLOADS = "uploads"
class A():
def __init__(self):
self.test()
def test(self):
global __UPLOADS
print __UPLOADS
def main():
a = A()
if __name__ == "__main__":
main()
本来想定义一个模块级的私有变量,一个underscore的_UPLOADS自然没有问题,一不小心用了两个,发现进坑了
报错: global name '_A__UPLOADS' is not defined
搜了半天也没找到module level上两个underscore是什么意思,倒是搜出来一些说
But try to avoid the __private form. I never use it. Trust me. If you use it, you WILL regret it later.
单纯的想知道怎么能访问到__UPLOADS
这是一条镜像帖。来源:北邮人论坛 / python / #6611同步于 2015/4/30
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
求助~module level私有变量
yyy8848
2015/4/30镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
应该是这个name nangling搞怪 http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python,等大神解释
私有变量一般说的是class variable吧,不是这种module level的变量吧
【 在 justhinking 的大作中提到: 】
: 私有变量轧压了,百度搜一搜
: 对python变量的access scope不是很理解...
是这个问题
访问的是_A__UPLOADS
看来还是等理解了这个name mangling里以后再用double underscore吧
【 在 luostar 的大作中提到: 】
: 应该是这个name nangling搞怪 http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python,等大神解释