Python Docstring Example


From Michelle Craig's Python presentation at #CEMC12:

The part in red is a Python docstring, and the included test cases can be used to test your function, as shown below.

def is_reverse_pair(s1,s2):
    '''
    (str,str) -> bool
    >>> is_reverse_pair("abc","cba")
    True
    >>> is_reverse_pair("abc","xxx")
    False
    '''
    s1_new = ""
    for i in range(0,len(s1)):
        s1_new = s1_new + s1[len(s1)-i-1]
    return s1_new == s2

To test the test cases in the docstring, type the following commands in the Python shell (after you have run your program):

>>> import doctest
>>> doctest.testmod()
TestResults(failed=0, attempted=2)

Comments

Popular posts from this blog

The Kiss of Death

Promoting "Eight Weeks To Web 2"

Shack on Route 62