mirror of
https://github.com/python/cpython.git
synced 2025-11-10 14:31:24 +00:00
add warning about situation where code may be executed twice, once when
module is __main__ and once when module is imported.
This commit is contained in:
parent
62f9d7c021
commit
1dc98c44fe
1 changed files with 10 additions and 0 deletions
|
|
@ -408,6 +408,16 @@ Simple fractions are also easier for people to understand, and that makes
|
||||||
for better documentation.
|
for better documentation.
|
||||||
\end{enumerate}
|
\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}
|
\subsection{Soapbox}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue