...
 
Commits (2)
...@@ -189,11 +189,13 @@ IndexError: list index out of range ...@@ -189,11 +189,13 @@ IndexError: list index out of range
\subsection{Improving our script for importing} \subsection{Improving our script for importing}
\begin{pframe} \begin{pframe}
\vspace{-.05cm}
\begin{itemize} \begin{itemize}
\item During a module import, Python executes all the statements inside the module. \item During a module import, Python executes all the statements inside the module.
\item To make our script work as a module (in the intended way), we need to \item To make our script work as a module (in the intended way), we need to
add a check whether the module is imported or not: add a check whether the module is imported or not:
\end{itemize} \end{itemize}
\vspace{-0.3cm}
\begin{pythoncode} \begin{pythoncode}
\begin{minted}{python} \begin{minted}{python}
if __name__ == '__main__': if __name__ == '__main__':
...@@ -203,10 +205,13 @@ if __name__ == '__main__': ...@@ -203,10 +205,13 @@ if __name__ == '__main__':
\end{minted} \end{minted}
\end{pythoncode} \end{pythoncode}
\begin{itemize} \begin{itemize}
\item Now try importing the module again. \item If the Python interpreter is running that module as the main program,
\begin{itemize} it sets the special \mintinline{python}{__name__} variable to have a value
\item What happens? Can you still use the module as a script? \mintinline{python}{"__main__"}.
\end{itemize} \item If this file is being imported from another module,
\mintinline{python}{__name__} will be set to the module's name.
\item Now try importing the module again. What happens? Can you still use the
module as a script?
\end{itemize} \end{itemize}
\end{pframe} \end{pframe}
...@@ -230,8 +235,9 @@ seq_toolbox.calc_gc_percent ...@@ -230,8 +235,9 @@ seq_toolbox.calc_gc_percent
<function seq\_toolbox.calc\_gc\_percent> <function seq\_toolbox.calc\_gc\_percent>
\end{pythonout} \end{pythonout}
\end{ipython} \end{ipython}
\pause \end{pframe}
\begin{pframe}
\begin{itemize} \begin{itemize}
\item By the way, remember we added docstring to the calc\_gc\_percent function? \item By the way, remember we added docstring to the calc\_gc\_percent function?
\item After importing our module, we can read up on how to use the function \item After importing our module, we can read up on how to use the function
......