doctest

The doctest module examines documentation strings for text fragments that look like interactive Python sessions. These fragments are then executed and verified to see if they produce the output shown. Here is a short example:

 def gcd(x,y): """ Computes the greatest common divisor of x and y. For example: >>> gcd(40,16) 8 >>> gcd(24,15) 3 >>> gcd(12,85) 1 Both arguments must be positive integers. >>> gcd(3.5,4.2) Traceback (most recent call last): ... TypeError: Arguments must be integers >>> gcd(-4,7) Traceback (most recent call last): ... ValueError: Arguments must be positive integers Long integers may also be used. In this case, the returned value is also a long. >>> gcd(23748928388L, 6723884L) 4L """ if not (isinstance(x,(int,long)) ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.