summaryrefslogtreecommitdiff
path: root/advanced_python/07_oop_basics.tex
blob: d6f4e24aa9c23a8f2ad54e315b5f9908d1196f34 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
\documentclass[14pt,compress,aspectratio=169]{beamer}

\input{macros.tex}

\title[Introduction to OOP]{Advanced Python}
\subtitle{Introduction to Object Oriented Programming }

\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{Background and caveats}
  \begin{itemize}
  \item You don't need OOP for everything!
  \item Helps with larger programs
  \item Appreciate it, when you write more code
  \item Very useful to organize/modularize your code
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{What are the benefits?}
  \begin{itemize}
  \item Create new data types
  \item Create objects to simulate a real problem
  \item Makes problem solving more natural and elegant
  \item Can be easier to understand
  \item Allows for code-reuse
  \end{itemize}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Example: Managing Talks}
  \begin{itemize}
  \item Want to manage list of talks at a conference
  \end{itemize}
  \begin{lstlisting}
talk = {'Speaker': 'Guido van Rossum',
        'Title': 'The History of Python'
        'Tags': 'python,history,C,advanced'}

def get_first_name(talk):
    return talk['Speaker'].split()[0]

def get_tags(talk):
    return talk['Tags'].split(',')
  \end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Issues}
  \begin{itemize}
  \item Not convenient to handle large number of talks
  \item Data separate from functions to manipulate talk
  \item Must pass \lstinline{talk} to each function
  \item No clear organization
  \item What if we had different kinds of talks?
  \end{itemize}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Creating a \typ{class}}
  \begin{lstlisting}
class Talk:
    """A class for the Talks."""
    def __init__(self, speaker, title, tags):
        self.speaker = speaker
        self.title = title
        self.tags = tags
  \end{lstlisting}
  \begin{itemize}
  \item \lstinline{class} is a keyword
  \item \lstinline{Talk} is the name of the class
  \item Notice the class doc-string
  \item Notice the indentation
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{methods and \lstinline{self}}
  \begin{lstlisting}
class Talk:
    # ...
    def get_speaker_firstname(self):
        return self.speaker.split()[0]

    def get_tags(self):
        return self.tags.split(',')
  \end{lstlisting}
  \begin{itemize}
  \item \lstinline{self} is a reference to the object itself
  \item The name \lstinline{self} is a convention
  \item Instance variables: \lstinline{self.name}
  \end{itemize}
\end{frame}

\begin{frame}[fragile, plain]
  \frametitle{Exercise: type this out}
  \vspace*{-0.1in}
  \begin{lstlisting}
class Talk:
    """A class for the Talks."""
    def __init__(self, speaker, title, tags):
        self.speaker = speaker
        self.title = title
        self.tags = tags

    def get_speaker_firstname(self):
        return self.speaker.split()[0]

    def get_tags(self):
        return self.tags.split(',')
  \end{lstlisting}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Instantiating a class to create objects}
  \begin{itemize}
  \item Creating objects or instances of a class is simple
  \item Call class, with arguments required by \lstinline{__init__}
  \end{itemize}
  \begin{lstlisting}
In []: bdfl = Talk('Guido van Rossum',
  ...:             'The History of Python',
  ...:             'python,history,C,advanced')
  \end{lstlisting}
  \begin{itemize}
  \item We can now call the methods of the Class
  \end{itemize}
  \begin{lstlisting}
In []: bdfl.get_tags()
In []: bdfl.get_speaker_firstname()
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\lstinline{__init__} method}
  \begin{itemize}
  \item A special method
  \item Called every time an instance of the class is created
  \end{itemize}
  \begin{lstlisting}
In []: print(bdfl.speaker)
In []: print(bdfl.tags)
In []: print(bdfl.title)
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Creating more instances}
  \begin{lstlisting}
In []: talk = Talk('Arun K',
  ...:      'Python in biosciences', 'python,bio')
In []: talk.get_tags()
In []: talk.tags

In []: type(talk)
Out[]: __main__.Talk
In []: type(bdfl)
\end{lstlisting}
\begin{itemize}
\item Note that \lstinline{talk} and \lstinline{bdfl} are different objects
\item Different values but same type
\item \lstinline{Talk} \alert{encapsulates} the data and behavior of a talk
\end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Changing/adding attributes}
\begin{lstlisting}
In []: talk.speaker = 'Arun K P'

In []: talk.tags = 'python,bioscience'
\end{lstlisting}
\pause
\begin{lstlisting}
# Adding attributes

In []: talk.x = 1

In []: talk.y = 'hello'
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Summary}
  \begin{itemize}
  \item Introduction to Object Oriented Programming
  \item A simple example
  \item Defining a \lstinline{class}
  \item Methods and attributes: encapsulation
  \item Changing/adding attributes
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Exercise: simplest class}
  \begin{block}{}
    Create the simplest possible class, called \lstinline{Simple}, which has
    no attributes or methods.  Try this out on your own IPython interpreters.
    You should be able to instantiate it as follows:
  \end{block}
\begin{lstlisting}
In []: s = Simple()
\end{lstlisting}
  \pause
  \begin{block}{Hint}
    Remember the \lstinline{pass} statement?
  \end{block}
\end{frame}


\begin{frame}[plain, fragile]
  \frametitle{Solution}
\begin{lstlisting}
class Simple:
    pass
\end{lstlisting}
  \vspace*{0.5in}

  That is all!
\end{frame}

\begin{frame}[fragile,plain, fragile]
  \frametitle{Exercise: \lstinline{Person} class 1}
  \begin{block}{}
    Create a simple \lstinline{Person} class with two attributes: a
    \lstinline{name} (a string) and an \lstinline{age} (a float).  To be used
    as follows:
  \end{block}
\begin{lstlisting}
In []: p = Person('Arun', 22.0)
\end{lstlisting}
\end{frame}


\begin{frame}[plain, fragile]
  \frametitle{Solution}
\begin{lstlisting}
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
\end{lstlisting}
\end{frame}

\begin{frame}[plain, fragile]
  \frametitle{Exercise: \lstinline{Person} class 2}
  \begin{block}{}
    Create a simple \lstinline{Person} class with two attributes a
    \lstinline{name} and an \lstinline{age}.  However, let the name default to
    the string \lstinline{'name'} and the age default to 0.0.
  \end{block}
\begin{lstlisting}
In []: p = Person()
In []: print(p.name, p.age)
name 0.0
\end{lstlisting}
\end{frame}

\begin{frame}[plain, fragile]
  \frametitle{Solution}
\begin{lstlisting}
class Person:
    def __init__(self, name='name', age=0.0):
        self.name = name
        self.age = age
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain, fragile]
  \frametitle{Exercise: simple method}
  \begin{block}{}
    Create a simple \lstinline{Person} class with two attributes as before.
    Add a method called \lstinline{birthday} which takes no arguments and
    which prints \lstinline{'HBD <name>'} and also increases the age by 1.
    Note that \lstinline{<name>} is the name of the instance.
  \end{block}

\begin{lstlisting}
In []: p = Person(name='Ram')
In []: p.birthday()
HBD Ram
In []: p.age
Out[]: 1.0
\end{lstlisting}
\end{frame}

\begin{frame}[plain, fragile]
  \frametitle{Solution}
\begin{lstlisting}
class Person:
    def __init__(self, name='name', age=0.0):
        self.name = name
        self.age = age

    def birthday(self):
        print('HBD', self.name)
        self.age += 1.0
\end{lstlisting}
\end{frame}



\end{document}