[ create a new paste ] login | about

Link: http://codepad.org/EPH6UZd0    [ raw code | output | fork ]

Python, pasted on Jul 13:
class Thing:
    """
    All of our internal uses of private members (__v, __f) will be unaffected
    by outsiders. The public member (f), can be messed with.

    >>> t = Thing(__v=0, __f=0, f=0)
    >>> t.variable()
    got it
    >>> t.privatefunc()
    still here
    >>> t.publicfunc()
    Exception raised -- f was overwritten with 0
    """
    def __init__(self, **kwargs):
        self.__v = 'got it'
        self.__dict__.update(kwargs)

    def variable(self):
        print self.__v

    def privatefunc(self):
        self.__f()
    def __f(self):
        print 'still here'

    def publicfunc(self):
        self.f()
    def f(self):
        print 'gone'


def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()
    


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
**********************************************************************
Line 11, in __main__.Thing
Failed example:
    t.publicfunc()
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.5/doctest.py", line 1212, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.Thing[3]>", line 1, in <module>
        t.publicfunc()
      Line 27, in publicfunc
        self.f()
    TypeError: 'int' object is not callable
**********************************************************************
1 items had failures:
   1 of   4 in __main__.Thing
***Test Failed*** 1 failures.


Create a new paste based on this one


Comments: