211 |
211 |
212 \begin{frame}[fragile] |
212 \begin{frame}[fragile] |
213 \frametitle{Functions: default arguments} |
213 \frametitle{Functions: default arguments} |
214 \small |
214 \small |
215 \begin{lstlisting} |
215 \begin{lstlisting} |
216 def ask_ok(prompt, retries=4, |
216 def ask_ok(prompt, complaint='Yes or no!'): |
217 complaint='Yes or no!'): |
|
218 while True: |
217 while True: |
219 ok = raw_input(prompt) |
218 ok = raw_input(prompt) |
220 if ok in ('y', 'ye', 'yes'): |
219 if ok in ('y', 'ye', 'yes'): |
221 return True |
220 return True |
222 if ok in ('n', 'no', 'nop', |
221 if ok in ('n', 'no', 'nop', |
223 'nope'): |
222 'nope'): |
224 return False |
223 return False |
225 retries = retries - 1 |
|
226 if retries < 0: |
|
227 raise IOError, 'bad user' |
|
228 print complaint |
224 print complaint |
|
225 |
|
226 ask_ok('?') |
|
227 ask_ok('?', '[Y/N]') |
229 \end{lstlisting} |
228 \end{lstlisting} |
230 \end{frame} |
229 \end{frame} |
231 |
230 |
232 \begin{frame}[fragile] |
231 \begin{frame}[fragile] |
233 \frametitle{Functions: keyword arguments} |
232 \frametitle{Functions: keyword arguments} |
234 \small |
233 \small |
235 \begin{lstlisting} |
234 \begin{lstlisting} |
236 def parrot(voltage, state='a stiff', |
235 def ask_ok(prompt, complaint='Yes or no!'): |
237 action='voom', type='Royal Blue'): |
236 while True: |
238 print "-- This parrot wouldn't", action, |
237 ok = raw_input(prompt) |
239 print "if you supply", voltage, "Volts." |
238 if ok in ('y', 'ye', 'yes'): |
240 print "-- Lovely plumage, the", type |
239 return True |
241 print "-- It's", state, "!" |
240 if ok in ('n', 'no', 'nop', |
242 |
241 'nope'): |
243 parrot(1000) |
242 return False |
244 parrot(action = 'VOOOOOM', voltage = 1000000) |
243 print complaint |
245 parrot('a thousand', |
244 |
246 state = 'pushing up the daisies') |
245 ask_ok(prompt='?') |
247 parrot('a million', 'bereft of life', 'jump') |
246 ask_ok(prompt='?', complaint='[Y/N]') |
248 \end{lstlisting} |
247 ask_ok(complaint='[Y/N]', prompt='?') |
249 \end{frame} |
248 \end{lstlisting} |
250 |
249 \inctime{15} |
251 \begin{frame}[fragile] |
|
252 \frametitle{Functions: arbitrary argument lists} |
|
253 \begin{itemize} |
|
254 \item Arbitrary number of arguments using \verb+*args+ or |
|
255 \verb+*whatever+ |
|
256 \item Keyword arguments using \verb+**kw+ |
|
257 \item Given a tuple/dict how do you call a function? |
|
258 \begin{itemize} |
|
259 \item Using argument unpacking |
|
260 \item For positional arguments: \verb+foo(*[5, 10])+ |
|
261 \item For keyword args: \verb+foo(**{'a':5, 'b':10})+ |
|
262 \end{itemize} |
|
263 \end{itemize} |
|
264 \end{frame} |
|
265 |
|
266 \begin{frame}[fragile] |
|
267 \begin{lstlisting} |
|
268 def foo(a=10, b=100): |
|
269 print a, b |
|
270 def func(*args, **keyword): |
|
271 print args, keyword |
|
272 # Unpacking: |
|
273 args = [5, 10] |
|
274 foo(*args) |
|
275 kw = {'a':5, 'b':10} |
|
276 foo(**kw) |
|
277 \end{lstlisting} |
|
278 \inctime{15} |
|
279 \end{frame} |
250 \end{frame} |
280 |
251 |
281 \subsection{Functional programming} |
252 \subsection{Functional programming} |
282 \begin{frame}[fragile] |
253 \begin{frame}[fragile] |
283 \frametitle{Functional programming} |
254 \frametitle{Functional programming} |
284 What is the basic idea?\\ |
255 What is the basic idea?\\ |
285 Why is it interesting?\\ |
256 Why is it interesting?\\ |
286 \typ{map, reduce, filter}\\ |
257 \typ{map, reduce, filter}\\ |
287 list comprehension\\ |
258 list comprehension\\ |
288 generators |
259 generators |
289 \inctime{15} |
260 \end{frame} |
290 \end{frame} |
261 |
|
262 \begin{frame}[fragile] |
|
263 \frametitle{List Comprehensions} |
|
264 Lets say we want to squares of all the numbers from 1 to 100 |
|
265 \begin{lstlisting} |
|
266 squares = [] |
|
267 for i in range(1, 100): |
|
268 squares.append(i * i) |
|
269 \end{lstlisting} |
|
270 \begin{lstlisting} |
|
271 # list comprehension |
|
272 squares = [i*i for i in range(1, 100)] |
|
273 \end{lstlisting} |
|
274 Which is more readable? |
|
275 \end{frame} |
|
276 |
|
277 \begin{frame}[fragile] |
|
278 \frametitle{List Comprehensions} |
|
279 What if you had a more complex function? |
|
280 Lets say we want squares of numbers from 1 to 100 ending in 1, 2, 5, 7 only |
|
281 \begin{lstlisting} |
|
282 squares = [] |
|
283 for i in range(1, 100): |
|
284 if i % 10 in [1, 2, 5, 7]: |
|
285 squares.append(i * i) |
|
286 \end{lstlisting} |
|
287 \begin{lstlisting} |
|
288 # list comprehension |
|
289 squares = [i*i for i in range(1, 100) |
|
290 if i % 10 in [1, 2, 5, 7]] |
|
291 \end{lstlisting} |
|
292 Which is more readable? |
|
293 \inctime{15} |
|
294 \end{frame} |
|
295 |
291 |
296 |
292 \subsection{Debugging} |
297 \subsection{Debugging} |
293 \begin{frame}[fragile] |
298 \begin{frame}[fragile] |
294 \frametitle{Errors} |
299 \frametitle{Errors} |
295 \begin{lstlisting} |
300 \begin{lstlisting} |