diff options
author | Prabhu Ramachandran | 2017-11-17 23:03:18 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2017-11-17 23:03:18 +0530 |
commit | b64d87addfc4b90e3a2c93763eff8faf1e7cee21 (patch) | |
tree | 6cacdbfbebdb120ce3cc1c4a86453d635038f49d /advanced_python/varargs_kwargs.tex | |
parent | 3b7ed43d8d159470654351c31a52f9edeaeaa60c (diff) | |
download | python-workshops-b64d87addfc4b90e3a2c93763eff8faf1e7cee21.tar.gz python-workshops-b64d87addfc4b90e3a2c93763eff8faf1e7cee21.tar.bz2 python-workshops-b64d87addfc4b90e3a2c93763eff8faf1e7cee21.zip |
Rename the files for easier navigation.
Diffstat (limited to 'advanced_python/varargs_kwargs.tex')
-rw-r--r-- | advanced_python/varargs_kwargs.tex | 181 |
1 files changed, 0 insertions, 181 deletions
diff --git a/advanced_python/varargs_kwargs.tex b/advanced_python/varargs_kwargs.tex deleted file mode 100644 index 29f7e88..0000000 --- a/advanced_python/varargs_kwargs.tex +++ /dev/null @@ -1,181 +0,0 @@ -\documentclass[14pt,compress,aspectratio=169]{beamer} - -\input{macros.tex} - -\title[*args, **kw]{Advanced Python} -\subtitle{Arbitrary arguments for functions} - -\author[FOSSEE] {The FOSSEE Group} - -\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {Mumbai, India} - -\begin{document} - -\begin{frame} - \titlepage -\end{frame} - -\begin{frame} - \frametitle{Overview} - \begin{itemize} - \item Advanced unpacking - \item Variable arguments - \item Arbitrary keyword arguments - \item Unpacking arguments - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Advanced unpacking} - \vspace*{-0.1in} -\begin{lstlisting} -In []: a, b = 1, 2 # We know this! -\end{lstlisting} - \pause - \begin{itemize} - \item In Python 3.x we can also do - \end{itemize} -\begin{lstlisting} -In []: first, *rest = range(5) - -In []: first -Out[]: 0 - -In []: first, *middle, last = range(5) - -In []: middle -Out[]: [1, 2, 3] -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Advanced unpacking ...} - \begin{itemize} - \item Also works for files - \end{itemize} -\begin{lstlisting} -In []: f = open('file.txt') - -In []: first, *ign, last = f.readlines() - -In []: f.close() -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Arbitrary positional arguments} - \vspace*{-0.1in} - \begin{itemize} - \item Arbitrary positional arguments: \lstinline{*arg} - \end{itemize} - -\begin{lstlisting} -In []: def func(a, b, *args): - ...: print(a, b, args) -\end{lstlisting} -\pause -\begin{lstlisting} -In []: func(1, 2) -1 2 () - -In []: func(1, 2, 'a') -1 2 ('a',) - -In []: func(1, 2, 3, 'a') -1 2 (3, 'a') -\end{lstlisting} - -\end{frame} - -\begin{frame}[fragile] - \frametitle{Arbitrary positional arguments} - - Nothing special about \lstinline{args}: - -\begin{lstlisting} -In []: def func(a, b, *rest): - ...: print(a, b, rest) - -\end{lstlisting} -Also is perfectly valid -\end{frame} - -\begin{frame}[fragile] - \frametitle{Arbitrary keyword arguments} - \vspace*{-0.1in} - \begin{itemize} - \item Keyword arguments using \lstinline{**kw} - \end{itemize} -\begin{lstlisting} -In []: def func(a, b='b', **kw): - ...: print(a, b, kw) -\end{lstlisting} - \pause -\begin{lstlisting} -In []: func(1) -1 b {} - -In []: func(1, 2) -1 2 {} - -In []: func(1, b=2) -1 2 {} -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Arbitrary keyword arguments} -\begin{lstlisting} -In []: def func(a, b='b', **k): - ...: print(a, b, k) - -In []: func(1, c=3) -1 b {'c': 3} - -In []: func(1, c=3, d=4) -1 b {'c': 3, 'd': 4} -\end{lstlisting} -\end{frame} - - -\begin{frame}[fragile] - \frametitle{Argument unpacking} -\begin{lstlisting} -In []: def func(a, b): - ...: print(a, b) - -In []: func(*[5, 10]) -5 10 - -In []: func(*(5, 10)) -5 10 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Argument unpacking ...} -\begin{lstlisting} -In []: def func(a=1, b=2): - ...: print(a, b) - -In []: func(**{'a':5, 'b':10}) -5 10 - -In []: func(**dict(a=5, b=10)) -5 10 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Summary} - \begin{itemize} - \item Advanced unpacking - \item Arbitrary positional arguments with \lstinline{*args} - \item Arbitrary keyword arguments with \lstinline{**kw} - \item Argument unpacking - \end{itemize} -\end{frame} - - -\end{document} |