From dbc71e582f663bc79366bce9f87201d1212ac96a Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Thu, 5 Feb 2015 16:29:50 +0530 Subject: Add standalone script slide. --- slides/basic_python/python_part2.tex | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/slides/basic_python/python_part2.tex b/slides/basic_python/python_part2.tex index 750a097..e0ca77e 100644 --- a/slides/basic_python/python_part2.tex +++ b/slides/basic_python/python_part2.tex @@ -191,6 +191,28 @@ def fib(n): # write Fibonacci series up to n \end{itemize} \end{frame} + +\begin{frame}[fragile] + \frametitle{Stand-alone scripts} +Consider a file \texttt{f.py}: +\footnotesize +\begin{lstlisting} +#!/usr/bin/env python +"""Module level documentation.""" +# First line tells the shell that it should use Python +# to interpret the code in the file. +def f(): + print "f" + +# Check if we are running standalone or as module. +# When imported, __name__ will not be '__main__' +if __name__ == '__main__': + # This is not executed when f.py is imported. + f() +\end{lstlisting} +\end{frame} + + \section{Exceptions} \begin{frame}{Motivation} -- cgit