0
考慮下面的代碼:具有雙下劃線的變量不能在課堂上使用?
def __g_fun():
pass
__a = 3
a=3
class Test(object):
def __init__(self):
global a,__a
print "locals:",locals()
print "global:",globals()
self.aa = __a
def fun(self):
return __g_fun()
t=Test()
t.fun()
輸出:
locals: {'self': <__main__.Test object at 0x7f53580d50d0>}
global: {'__g_fun': <function __g_fun at 0x7f53580c52a8>, 'a': 3, '__a': 3, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'test.py', '__package__': None, 'Test': <class '__main__.Test'>, '__name__': '__main__', '__doc__': None}
Traceback (most recent call last):
File "test.py", line 19, in <module>
t=Test()
File "test.py", line 11, in __init__
self.aa = __a
NameError: global name '_Test__a' is not defined
難道雙下劃線變量不能在課堂上使用的情況下?