diff options
author | Prabhu Ramachandran | 2017-11-17 01:03:41 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2017-11-17 01:03:41 +0530 |
commit | e93ca72debbcadbe357900c701f45575bb2142be (patch) | |
tree | be0998b2cb3cb2576bc70cde2361a10fad9c5386 /advanced_python | |
parent | 9555331147e61e648c888177b350303c67c0c55d (diff) | |
download | python-workshops-e93ca72debbcadbe357900c701f45575bb2142be.tar.gz python-workshops-e93ca72debbcadbe357900c701f45575bb2142be.tar.bz2 python-workshops-e93ca72debbcadbe357900c701f45575bb2142be.zip |
Add some basic material on OOP.
Diffstat (limited to 'advanced_python')
-rw-r--r-- | advanced_python/Makefile | 4 | ||||
-rw-r--r-- | advanced_python/README.md | 14 | ||||
-rw-r--r-- | advanced_python/macros.tex | 2 | ||||
-rw-r--r-- | advanced_python/oop_basics.tex | 191 | ||||
-rw-r--r-- | advanced_python/oop_inheritance.tex | 219 |
5 files changed, 427 insertions, 3 deletions
diff --git a/advanced_python/Makefile b/advanced_python/Makefile index 4751a6e..782fe89 100644 --- a/advanced_python/Makefile +++ b/advanced_python/Makefile @@ -9,7 +9,9 @@ SLIDES= intro.pdf \ varargs_kwargs.pdf \ only_kwargs.pdf \ closures.pdf \ - practice_functions.pdf + practice_functions.pdf \ + oop_basics.pdf \ + oop_inheritance.pdf all: $(SLIDES) diff --git a/advanced_python/README.md b/advanced_python/README.md index 8237ac3..e7fb8cd 100644 --- a/advanced_python/README.md +++ b/advanced_python/README.md @@ -11,8 +11,20 @@ and requires the use of yaksh for practice, self-assessment, and assessment. ## Pre-requisites - Students should have completed the basic Python programming material. -- One should have a Python 3.x installation. +- One should have a Python 3.x installation with IPython installed. - Use a reasonable editor, Canopy will work. - If one desires a more advanced editor, I suggest VS Code (https://code.visualstudio.com/) which is free, open source, and very powerful. + + +## Contents + +- Introduction. +- Expressions, statements, names, and objects. +- Functions: arbitrary positional and keyword arguments. +- Functions: only-keyword arguments. +- Closures. +- Practice exercises for functions. +- Object oriented programming: basics +- Inheritance diff --git a/advanced_python/macros.tex b/advanced_python/macros.tex index 37a70ba..0dfe38b 100644 --- a/advanced_python/macros.tex +++ b/advanced_python/macros.tex @@ -2,7 +2,7 @@ % Modified from: generic-ornate-15min-45min.de.tex \mode<presentation> { - \usetheme{Warsaw} + \usetheme{Madrid} \useoutertheme{infolines} \setbeamercovered{invisible} } diff --git a/advanced_python/oop_basics.tex b/advanced_python/oop_basics.tex new file mode 100644 index 0000000..1da56fb --- /dev/null +++ b/advanced_python/oop_basics.tex @@ -0,0 +1,191 @@ +\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{Summary} + \begin{itemize} + \item Introduction to Object Oriented Programming + \item A simple example + \item Defining a \lstinline{class} + \item Methods and attributes: encapsulation + \end{itemize} +\end{frame} + + + +\end{document} diff --git a/advanced_python/oop_inheritance.tex b/advanced_python/oop_inheritance.tex new file mode 100644 index 0000000..0077556 --- /dev/null +++ b/advanced_python/oop_inheritance.tex @@ -0,0 +1,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} |