##
## I happened to stumble upon name mangling in a class
## when trying to write a unit test of a function that maintained
## a value in a variable named __FOO
##
## attempts to access the variable from inside the class would report
## 'module' object has no attribute <classname>__<variablename>'
##
## http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python
##
$ cat myutil.py
_SECRET = {'key1': 'val1'}
__SUPER_SECRET = {'skey1': 'sval1'}
BAR = {'bar1': '1val'}
$ cat test_myutil.py
from unittest import TestCase
import myutil
class TestFoo(TestCase):
def test_secret(self):
x = myutil._SECRET
def test_super_supersecret(self):
x = myutil.__SUPER_SECRET
def test_x():
x = myutil.__SUPER_SECRET
$ nosetests -v test_myutil.py
test_secret (test_myutil.TestFoo) ... ok
test_super_supersecret (test_myutil.TestFoo) ... ERROR
test_myutil.test_x ... ok
======================================================================
ERROR: test_super_supersecret (test_myutil.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/mytest/test_myutil.py", line 9, in test_super_supersecret
x = myutil.__SUPER_SECRET
AttributeError: 'module' object has no attribute '_TestFoo__SUPER_SECRET'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (errors=1)
shell returned 1