Python: testing

   

I am impressed! When writing the header of a function, why not simply write an example and automatically test the function?

#!/usr/bin/env python

import doctest

def fact(n, stop=1, r=1):
""" An iterative factorial function.
n!     = n*(n-1)*(n-2)*..*(n-(n-stop-1))*r
stop    fact(5,3)= 5*4
r        fact(5,3,10)=5*4*10
>>> fact(5,3,10)
200
>>> fact(4)
24
"""

while n > stop :
r *= n
n -= 1
return r    

def main():
doctest.testmod()
return 0

if __name__ == '__main__': main()

More here: http://docs.python.org/tutorial/stdlib.html#quality-control

... read more >>> firmit.wordpress.com
None