Ubuntu Pastebin

Paste from smoser at Tue, 17 Nov 2015 17:35:14 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
##
## 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
Download as text