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
|
\documentclass[14pt,compress,aspectratio=169]{beamer}
\input{macros.tex}
\title[OOP Inheritance]{Advanced Python}
\subtitle{Object Oriented Programming: Inheritance}
\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 a new \lstinline{Talk} class
\item Puts together data and methods
\item Can make instances of the class
\item Each instance has its own data
\item Objects encapsulate data and behavior
\end{itemize}
\end{frame}
\begin{frame}[fragile, plain]
\frametitle{Recap: the Talk class}
\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{lstlisting}
In []: bdfl = Talk('Guido van Rossum',
...: 'The History of Python',
...: 'python,history,C,advanced')
In []: bdfl.get_tags()
In []: bdfl.get_speaker_firstname()
In []: bdfl.tags
In []: type(bdfl)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Classes: the big picture}
\begin{itemize}
\item Lets you create new data types
\item Class is a template for an object belonging to that class
\item Instantiating a class creates an instance (an object)
\item An instance encapsulates the state (data) and behavior
(methods)
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Objects and Methods}
\begin{itemize}
\item Objects group data with functions
\item Everything in Python is an object
\item Strings, lists, functions and even modules
\end{itemize}
\begin{lstlisting}
s = "Hello World"
s.lower()
l = [1, 2, 3, 4, 5]
l.append(6)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Classes}
\begin{lstlisting}
In []: s = "Hello World"
In []: type(s)
\end{lstlisting}
\begin{itemize}
\item A new string, comes along with methods
\item A template or a blue-print, where these definitions lie
\item This blue print for building objects is called a
\lstinline{class}
\item \lstinline{s} is an object of the \lstinline{str} class
\item An object is an ``instance'' of a class
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Classes: inheritance}
\begin{itemize}
\item Allows you to define an inheritance hierarchy
\begin{itemize}
\item ``A Honda car \alert{is a} car.''
\item ``A car \alert{is an} automobile.''
\item ``A Python \alert{is a} reptile.''
\end{itemize}
\item All data/behavior of a car should be there in a ``Honda'' car
\item All data/behavior associated with a reptile ought to inhere in a snake
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Inheritance}
\vspace*{-0.1in}
\begin{itemize}
\item Suppose, we wish to write a \lstinline{Tutorial} class
\item It's almost same as \lstinline{Talk} except for minor differences
\item We can ``inherit'' from \lstinline{Talk}
\end{itemize}
\pause
\begin{lstlisting}
class Tutorial(Talk):
"""A class for the tutorials."""
def __init__(self, speaker, title, tags,
needs_computer=True):
super().__init__(speaker, title, tags)
self.needs_computer = needs_computer
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Inheritance}
\vspace*{-0.1in}
\begin{itemize}
\item Modified \lstinline{__init__} method
\item Inherits: \lstinline{get_tags} and
\lstinline{get_speaker_firstname}
\end{itemize}
\begin{lstlisting}
tut = Tutorial('Travis Oliphant',
'Numpy Basics',
'numpy,python,beginner')
tut.get_speaker_firstname()
tut.needs_computer
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Some points}
\begin{itemize}
\item \lstinline{Tutorial} is a subclass (or child) of \lstinline{Talk}
\item \lstinline{Talk} is the base class (or parent class)
\item Only the \lstinline{__init__} has been changed
\item This is called overriding
\item \lstinline{super} ensures that the parent class is called
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Exercise: try this}
\begin{lstlisting}
class Tutorial(Talk):
def __init__(self, speaker, title, tags,
needs_computer=True):
self.needs_computer = needs_computer
In []: t = Tutorial('G v R', 'Python', 'py,tut')
In []: t.needs_computer
In []: t.speaker
In []: t.get_speaker_firstname())
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Inheritance}
\begin{itemize}
\item A \lstinline{Tutorial} is a \lstinline{Talk}
\item A \lstinline{Talk} is not a \lstinline{Tutorial}
\item The sentence above should make sense!
\item A \lstinline{Cat} is an \lstinline{Animal}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Incorrect inheritance}
\begin{itemize}
\item A \lstinline{Talk} is a \lstinline{Speaker} or
\item A \lstinline{Speaker} is a \lstinline{Room}
\item Something is wrong in your OO design!
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Summary}
\begin{itemize}
\item Inheritance
\item The importance of making sense
\item The \lstinline{super} function
\end{itemize}
\end{frame}
\end{document}
|