How to make this doctest work?

Terry Reedy tjreedy at udel.edu
Tue May 11 14:42:00 EDT 2010


On 5/11/2010 5:29 AM, Xie&Tian wrote:
> Hello
>
> I ran across this accidentally and wonders how to make the doctest in
> following code snippet work:
>
>
> import doctest
>
> def a():
>       """
>      >>>  a = '\\r\\n'
>      >>>  print a
>
>
>      No matter how many blank lines I add here, it just can't get enough -_-
>      """
>      pass
>
> doctest.testmod()
>
>
>
> ps: I want variable "a" to be '\r\n', but python kept telling me
>          ValueError: line 4 of the docstring has inconsistent leading
> whitespace: "'"
>      Why can't doctest module deal with statement "a='\r\n'"?

You should post (copy and paste) entire error messages.
The problem is with the print statement, not the assignment.
I would prefix the docstring with r instead doubling \s within it.
The point of doctest is to imitate what one would get with the 
interactive interpreter. However, literally entering what you have 
within the doc string does (3.1)

 >>> a = '\\r\\n'
 >>> print(a)
\r\n
 >>>

Terry Jan Reedy






More information about the Python-list mailing list