diff options
-rw-r--r-- | advanced_python/8_oop_inheritance.tex | 107 | ||||
-rw-r--r-- | advanced_python/9_oop_containership.tex | 45 | ||||
-rw-r--r-- | advanced_python/macros.tex | 3 |
3 files changed, 146 insertions, 9 deletions
diff --git a/advanced_python/8_oop_inheritance.tex b/advanced_python/8_oop_inheritance.tex index 183eff5..657dfad 100644 --- a/advanced_python/8_oop_inheritance.tex +++ b/advanced_python/8_oop_inheritance.tex @@ -2,8 +2,6 @@ \input{macros.tex} -\newcommand{\py}[1]{\lstinline{#1}} - \title[OOP Inheritance]{Advanced Python} \subtitle{Object Oriented Programming: Inheritance} @@ -185,7 +183,7 @@ In []: t.needs_computer In []: t.speaker -In []: t.get_speaker_firstname()) +In []: t.get_speaker_firstname() \end{lstlisting} \end{frame} @@ -218,6 +216,9 @@ In []: t.get_speaker_firstname()) \end{itemize} \end{frame} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Exercises + \begin{frame}[fragile] \frametitle{Exercise: \lstinline{Animal} class} \begin{block}{} @@ -262,7 +263,7 @@ class Animal(object): \begin{frame}[fragile] \frametitle{Exercise: \lstinline{Cat} class} \begin{block}{} - Create \lstinline{Cat} class which derives from the \lstinline{Animal} + Create a \lstinline{Cat} class which derives from the \lstinline{Animal} class \alert{override} \lstinline{greet} return \lstinline{'<name> says meow'}. \end{block} @@ -293,12 +294,31 @@ class Cat(Animal): \end{frame} \begin{frame}[plain, fragile] + \frametitle{Observation: order matters} +\begin{lstlisting} +class Cat(Animal): + def greet(self): + return self.name + ' says meow' + +class Animal: + def __init__(self, name): + self.name = name + def greet(self): + return self.name + ' says greet' + +\end{lstlisting} + \begin{itemize} + \item Will the above code work? Explore this and see what error you get. + \end{itemize} +\end{frame} + + +\begin{frame}[plain, fragile] \frametitle{Exercise: \lstinline{Mammal} class} \begin{block}{} Create a \lstinline{Mammal} class that derives from \lstinline{Animal} - that takes an additional argument that takes an additional argument - specifying the number of legs (an integer) and stores it in the attribute - \lstinline{legs}. + that takes an additional argument specifying the number of legs (an + integer) and stores it in the attribute \lstinline{legs}. \end{block} \begin{lstlisting} @@ -321,6 +341,7 @@ class Mammal(Animal): \end{lstlisting} \end{frame} + \begin{frame}[plain, fragile] \frametitle{Solution alternative} \begin{lstlisting} @@ -336,6 +357,26 @@ class Mammal(Animal): \end{frame} \begin{frame}[plain, fragile] + \frametitle{More on \typ{super}} +We can also do: +\begin{lstlisting} +class Mammal(Animal): + def __init__(self, name, legs): + super(Mammal, self).__init__(name) + +In []: super? + +In []: m = Mammal('dog', 4) +In []: super(Mammal, m) + +In []: super(Mammal, m).greet() + +In []: super(Animal, m).greet() # ?? +\end{lstlisting} +\end{frame} + + +\begin{frame}[plain, fragile] \frametitle{Exercise: \lstinline{Human} class} \begin{block}{} Create a \lstinline{Human} class that derives from \lstinline{Mammal} @@ -434,5 +475,57 @@ class Human(Mammal): \end{itemize} \end{frame} +\begin{frame}[fragile] + \frametitle{Exercise: class in a module} + + \begin{block}{} + Put the \py{Animal, Mammal, Human} classes inside a Python file, called + \texttt{oop.py} somewhere convenient in your computer. Add the following + code at the bottom. + \end{block} +\begin{lstlisting} +print(__name__) +h = Human('Sam') +print(h.greet()) +print(h.legs) +print(type(h)) +\end{lstlisting} + + \begin{itemize} + \item Execute this module using \texttt{python oop.py} + \item Import the \py{Human} class from IPython + \end{itemize} +\end{frame} + +\begin{frame}[plain, fragile] + \frametitle{Exercise: overriding a \typ{list} method} + \begin{block}{} + Create a subclass of the standard Python \py{list} called \py{MyList} and + override just the \py{append} method to print the item appended as + \py{'appending <item>'} and then call the parent append so as to + internally append the item. The usage should be as follows: + \end{block} + +\begin{lstlisting} +In []: l = MyList() +In []: l.append(1) +appending 1 +In []: l +Out[]: [1] +\end{lstlisting} +\end{frame} + + +\begin{frame}[plain, fragile] + \frametitle{Solution} +\begin{lstlisting} +class MyList(list): + def append(self, item): + print('appending', item) + super().append(item) +\end{lstlisting} +\end{frame} + + \end{document} diff --git a/advanced_python/9_oop_containership.tex b/advanced_python/9_oop_containership.tex index 5154a60..6434819 100644 --- a/advanced_python/9_oop_containership.tex +++ b/advanced_python/9_oop_containership.tex @@ -131,7 +131,7 @@ class Conference: \begin{frame} \frametitle{Some more examples} \begin{itemize} - \item OOP for a \lstinline{Company} class + \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, ... @@ -160,5 +160,48 @@ class Conference: \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} diff --git a/advanced_python/macros.tex b/advanced_python/macros.tex index 0dfe38b..b5d16e3 100644 --- a/advanced_python/macros.tex +++ b/advanced_python/macros.tex @@ -41,9 +41,10 @@ \newcommand{\typ}[1]{\textbf{\texttt{{#1}}}} - \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } +\newcommand{\py}[1]{\lstinline{#1}} + %%% This is from Fernando's setup. % \usepackage{color} % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |