]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
add warning about situation where code may be executed twice, once when
authorSkip Montanaro <skip@pobox.com>
Fri, 8 Jun 2001 14:40:28 +0000 (14:40 +0000)
committerSkip Montanaro <skip@pobox.com>
Fri, 8 Jun 2001 14:40:28 +0000 (14:40 +0000)
module is __main__ and once when module is imported.

Doc/lib/libdoctest.tex

index 6d19259d257eafc0f8dbb5509b74a0712277be17..30833da181a14c13923bc013031c5eb74358aa9a 100644 (file)
@@ -408,6 +408,16 @@ Simple fractions are also easier for people to understand, and that makes
 for better documentation.
 \end{enumerate}
 
+\item Be careful if you have code that must only execute once.
+
+If you have module-level code that must only execute once, a more foolproof
+definition of \function{_test} is
+
+\begin{verbatim}
+def _test():
+    import doctest, sys
+    doctest.testmod(sys.modules["__main__"])
+\end{verbatim}
 
 \subsection{Soapbox}