summaryrefslogtreecommitdiff
path: root/advanced_python/09_oop_containership.tex
blob: 64348192e048a4d8421b24e931f2e91075e24d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
\documentclass[14pt,compress,aspectratio=169]{beamer}

\input{macros.tex}

\title[OOP containership]{Advanced Python}
\subtitle{Object Oriented Programming: containership }

\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{Recap}
  \begin{itemize}
  \item Created \lstinline{Talk} and \lstinline{Tutorial} classes
  \item Know how to create objects: instantiation of a class
    \vspace*{0.5in}

  \item Let us create a conference!
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Conference class}
  \vspace*{-0.1in}
\begin{lstlisting}
class Conference:
    def __init__(self, talks=None):
        if talks is None:
            talks = []
        self.talks = talks

    def add_talk(self, talk):
        self.talks.append(talk)

    def schedule(self):
        for talk in self.talks:
            print(talk.speaker, talk.title)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Using this}
\begin{lstlisting}
In []: c = Conference()

In []: t = Talk('Guido', 'Python4', 'py,core')

In []: c.add_talk(t)

In []: c.schedule()

\end{lstlisting}
  \begin{itemize}
  \item Easy to read
  \item Nicely split up
  \item Reflects the real world

  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{But why not this?}
\begin{lstlisting}
class Conference:
    def __init__(self, talks=[]):
        self.talks = talks
\end{lstlisting}
  \begin{itemize}
  \item Easier
  \item Less code
  \item Let's try it!
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Gotcha!}
\begin{lstlisting}
In []: c1 = Conference()
In []: c1.add_talk(1)
In []: c1.talks

In []: c2 = Conference()
In []: c2.talks

\end{lstlisting}

  \begin{itemize}
  \item What is going on?
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Beware of mutable default arguments!}
  \begin{itemize}
  \item Default arguments are evaluated once
  \item When the method/function is defined
  \item So, if the object is mutable we have a problem
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Which is why we have this}
\begin{lstlisting}
class Conference:
    def __init__(self, talks=None):
        if talks is None:
            talks = []
        self.talks = talks
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Containership}
  \begin{itemize}
  \item \lstinline{Conference} class manages all the talks
  \item It contains the talks and tutorials
  \item Allows us to build complexity by \alert{composition}
  \item Hierarchical organization
  \item Allows us to mirror the real objects in code
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Some more examples}
  \begin{itemize}
  \item OOP for a \lstinline{Company}
  \item OOP for a \lstinline{Bank}
  \item OOP for a \lstinline{School} or \lstinline{College}
  \item OOP for a cell phone, computer, ...
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Design guidelines}
  \begin{itemize}
  \item Important to make sense
  \item Should be easy to explain the relationships and responsibilities of
    each class
  \item Inheritance should ``read'' correctly and make sense
  \item No object should be too responsible
  \item Functionality should be suitably delegated
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Summary}
  \begin{itemize}
  \item Creating a more complex class
  \item Containing other objects
  \item Issue with mutable default arguments
  \item Important design guidelines
  \end{itemize}
\end{frame}

\begin{frame}[plain, fragile]
  \frametitle{Exercise: Create a Zoo}
  \begin{block}{}
    Create a \py{Zoo} class which allows someone to add any number of animals.
    This means it should work for \py{Animal, Mammal, Human} objects. It
    should have a single \py{greet} method which calls the greet method of all
    of the animals it has and prints that:
  \end{block}

\begin{lstlisting}
In []: b = Animal('crow')
In []: m = Mammal('dog', legs=4)
In []: h = Human('Abhinav')
In []: z = Zoo(b, m, h)
In []: z.greet()
crow says greet
dog says greet
Abhinav says hello
\end{lstlisting}
\end{frame}


\begin{frame}[plain, fragile]
  \frametitle{Solution}
\begin{lstlisting}
class Zoo:
    def __init__(self, *animals):
        self.animals = list(animals)

    def greet(self):
        for animal in self.animals:
            print(animal.greet())
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Observations}
  \begin{itemize}
  \item Notice the abstraction of \py{Animal}
  \item The \py{Zoo} will work with any object that has the \py{greet} method
  \item That is, any \py{Animal}
  \end{itemize}
\end{frame}

\end{document}