Skip to content

Commit 13cec07

Browse files
start of [presentation
1 parent ee2a483 commit 13cec07

File tree

3 files changed

+209
-8
lines changed

3 files changed

+209
-8
lines changed

week-07/presentation-week-07.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ \section{Multiple Inheritance}
519519
\begin{verbatim}
520520
class A(B):
521521
def __init__(self, *args, **kwargs)
522-
super(B, self).__init__(self, *argw, **kwargs)
522+
super(A, self).__init__(self, *argw, **kwargs)
523523
...
524524
\end{verbatim}
525525

week-08/presentation-week-08.pdf

108 KB
Binary file not shown.

week-08/presentation-week-08.tex

Lines changed: 208 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
\documentclass{beamer}
22
%\usepackage[latin1]{inputenc}
33
\usetheme{Warsaw}
4-
\title[Intro to Python: Week 1]{Introduction to Python\\ Topics}
4+
\title[Intro to Python: Week 8]{Introduction to Python\\ More OO}
55
\author{Christopher Barker}
66
\institute{UW Continuing Education}
7-
\date{October 15, 2013}
7+
\date{November 19, 2013}
88

99
\usepackage{listings}
1010
\usepackage{hyperref}
@@ -73,18 +73,155 @@ \section{Review/Questions}
7373

7474
\end{frame}
7575

76-
%########################
77-
\section{First Section}
76+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77+
\section{Special Methods}
7878

79-
\begin{frame}[fragile]{topic}
79+
\begin{frame}[fragile]{special methods}
8080

81-
{\Large Some Stuff}
81+
{\Large Python's Duck typing:}
8282

83+
\vfill
84+
{\Large Defining special (or magic) methods in your classes is how you make
85+
your class act like standard classes}
86+
87+
\end{frame}
88+
89+
\begin{frame}[fragile]{special methods}
90+
91+
{\Large We've seen at least one:}
92+
93+
\begin{verbatim}
94+
__init__
95+
\end{verbatim}
96+
97+
\vfill
98+
{\Large it's all in the double underscores...}
99+
100+
\vfill
101+
{\Large Pronounced ``dunder'' (or ``under-under'') }
102+
103+
\vfill
104+
{\Large try: \verb|dir(2)| or \verb|dir(list)|}
105+
106+
\end{frame}
107+
108+
\begin{frame}[fragile]{special methods}
109+
110+
{\Large Emulating Numeric types}
111+
112+
\begin{verbatim}
113+
object.__add__(self, other)
114+
object.__sub__(self, other)
115+
object.__mul__(self, other)
116+
object.__floordiv__(self, other)
117+
object.__mod__(self, other)
118+
object.__divmod__(self, other)
119+
object.__pow__(self, other[, modulo])
120+
object.__lshift__(self, other)
121+
object.__rshift__(self, other)
122+
object.__and__(self, other)
123+
object.__xor__(self, other)
124+
object.__or__(self, other)¶
125+
\end{verbatim}
126+
127+
\end{frame}
128+
129+
\begin{frame}[fragile]{special methods}
130+
131+
{\Large Emulating container types:}
132+
133+
\begin{verbatim}
134+
object.__len__(self)
135+
object.__getitem__(self, key)
136+
object.__setitem__(self, key, value)
137+
object.__delitem__(self, key)
138+
object.__iter__(self)
139+
object.__reversed__(self)
140+
object.__contains__(self, item)
141+
object.__getslice__(self, i, j)
142+
object.__setslice__(self, i, j, sequence)
143+
object.__delslice__(self, i, j)
144+
\end{verbatim}
145+
146+
\end{frame}
147+
148+
\begin{frame}[fragile]{special methods}
149+
150+
{\Large Example -- to define addition:}
151+
152+
\begin{verbatim}
153+
def __add__(self, v):
154+
"""
155+
redefine + as element-wise vector sum
156+
"""
157+
assert len(self) == len(v)
158+
return vector([x1 + x2 for x1, x2 in zip(self, v)])
159+
\end{verbatim}
160+
161+
( from a nice complete example in \verb|code/vector.py| )
162+
163+
\end{frame}
164+
165+
166+
\begin{frame}[fragile]{special methods}
167+
168+
{\Large You get the idea...}
169+
170+
\vfill
171+
{\Large You only need to define the ones that are going to get used}
172+
173+
\vfill
174+
{\Large But you probably want to define at least these:}
175+
176+
\vfill
177+
\verb|object.__str__|: Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object.
178+
179+
\vfill
180+
\verb|object.__repr__|: Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object.
181+
182+
\end{frame}
183+
184+
\begin{frame}[fragile]{special methods}
185+
186+
\vfill
187+
{\Large When you want your class to act like a "standard" class in some way:}
188+
189+
\vfill
190+
{\Large Look up the magic methods you need and define them}
191+
192+
\vfill
193+
\url{http://docs.python.org/reference/datamodel.html#special-method-names}
194+
195+
\vfill
196+
\url{http://www.rafekettler.com/magicmethods.html}
197+
\end{frame}
198+
199+
\begin{frame}[fragile]{LAB}
200+
201+
{\Large Write a ``Circle'' class:}
202+
203+
204+
\vfill
205+
{\large A Circle has a radius and can compute its area:}
83206
\begin{verbatim}
84-
sample code
207+
In [2]: c = Circle(3)
208+
In [3]: c.radius
209+
Out[3]: 3
210+
In [4]: c.get_area()
211+
Out[4]: 28.274333882308138
212+
In [5]: print c
213+
Circle Object with radius: 3.000000
85214
\end{verbatim}
215+
{\large Write an \verb|__add__| method so you can add two circles }
216+
217+
\vfill
218+
{\large Have \verb|__str__| and \verb|__repr__| methods }
86219

220+
\vfill
221+
{\large Extra credit: also compare them... (\verb|c1 > c2|, etc)}
87222

223+
\vfill
224+
{\large \verb|code/circle.py| and \verb|code/test_circle.py|}
88225
\end{frame}
89226

90227

@@ -112,6 +249,70 @@ \section{First Section}
112249
\end{frame}
113250

114251

252+
253+
%-------------------------------
254+
\begin{frame}[fragile]{Wrap Up}
255+
256+
{\LARGE Thinking OO in Python:}
257+
258+
\vfill
259+
{\large Think about what makes sense for your code:}
260+
\begin{itemize}
261+
\item {\large Code re-use}
262+
\item {\large Clean APIs}
263+
\item {\large ... }
264+
\end{itemize}
265+
266+
\vfill
267+
{\large Don't be a slave to what OO is \emph{supposed} to look like. }
268+
269+
\vfill
270+
{\large Let OO work for you, not \emph{create} work for you}
271+
272+
\end{frame}
273+
274+
275+
%-------------------------------
276+
\begin{frame}[fragile]{Wrap Up}
277+
278+
{\Large OO in Python:}
279+
280+
\vfill
281+
{\Large The Art of Subclassing}: Raymond Hettinger
282+
283+
\vfill
284+
{\small \url{http://pyvideo.org/video/879/the-art-of-subclassing}}
285+
286+
\vfill
287+
''classes are for code re-use -- not creating taxonomies''
288+
289+
\vfill
290+
{\Large Stop Writing Classes}: Jack Diederich
291+
292+
\vfill
293+
{\small \url{http://pyvideo.org/video/880/stop-writing-classes}}
294+
295+
\vfill
296+
``If your class has only two methods -- and one of them is \verb|__init__|
297+
-- you don't need a class ''
298+
\end{frame}
299+
300+
301+
302+
303+
%-------------------------------
304+
\begin{frame}[fragile]{Homework}
305+
306+
{\Large Finish the labs.}
307+
308+
{\Large You should have a good start on your project by the end of this week}
309+
310+
\end{frame}
311+
312+
313+
314+
315+
115316
%-------------------------------
116317
\begin{frame}[fragile]{Homework}
117318

0 commit comments

Comments
 (0)