summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-11-27 01:23:26 +0530
committerPrabhu Ramachandran2017-11-27 01:23:26 +0530
commit205ea4d7aa19ef34b112fd5081000a3fd21b95bd (patch)
treeb437a945190d8e824aacdb9671828a5e68719193
parent87e2b74b18194060ed14e7744ed59d46f3c1b98a (diff)
downloadpython-workshops-205ea4d7aa19ef34b112fd5081000a3fd21b95bd.tar.gz
python-workshops-205ea4d7aa19ef34b112fd5081000a3fd21b95bd.tar.bz2
python-workshops-205ea4d7aa19ef34b112fd5081000a3fd21b95bd.zip
Adding slides on OOP class attributes and misc.
-rw-r--r--advanced_python/10_oop_classattr.tex204
-rw-r--r--advanced_python/11_oop_misc.tex154
-rw-r--r--advanced_python/Makefile4
3 files changed, 361 insertions, 1 deletions
diff --git a/advanced_python/10_oop_classattr.tex b/advanced_python/10_oop_classattr.tex
new file mode 100644
index 0000000..4548de1
--- /dev/null
+++ b/advanced_python/10_oop_classattr.tex
@@ -0,0 +1,204 @@
+\documentclass[14pt,compress,aspectratio=169]{beamer}
+
+\input{macros.tex}
+
+\title[OOP: Class Attributes]{Advanced Python}
+\subtitle{Object Oriented Programming: class attributes}
+
+\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 Classes for encapsulation
+ \item Inheritance
+ \item \py{super,isinstance,issubclass}
+ \item Containership
+ \vspace*{0.3in}
+ \item Look at class attributes/methods
+ \end{itemize}
+\end{frame}
+
+
+\begin{frame}[fragile]
+ \frametitle{Class/instance attributes}
+ \begin{itemize}
+ \item So far: instance attributes
+ \end{itemize}
+\begin{lstlisting}
+class A:
+ x = 1
+
+In []: a = A()
+In []: a.x
+Out[]: 1
+
+In []: b = A()
+In []: assert b.x == a.x
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Class/instance attributes}
+\begin{lstlisting}
+class A:
+ x = 1
+
+In []: a, b = A(), A()
+
+In []: A.x = 2
+In []: b.x
+Out[]: 2
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Class/instance attributes: beware}
+\begin{lstlisting}
+class A:
+ x = 1
+
+In []: a = A()
+In []: a.x = 1
+In []: A.x = 2
+In []: a.x
+Out[]: ??
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Understanding the scope}
+ \begin{itemize}
+ \item If \py{a.x} is not an instance attribute
+ \item Python looks in the class for the attribute!
+ \pause
+ \vspace*{0.25in}
+ \item So \py{a.x = 1} sets an instance attribute!
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile,fragile]
+ \frametitle{Class attribute scope}
+\begin{lstlisting}
+class A:
+ x = 1
+ def f(self):
+ return self.x + 1
+
+In []: a = A()
+In []: a.f()
+\end{lstlisting}
+ \pause
+\begin{lstlisting}
+Out[]: 2
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[fragile]
+ \frametitle{Class methods}
+ \begin{itemize}
+ \item Can we have methods on the class?
+ \end{itemize}
+\begin{lstlisting}
+class A:
+ @classmethod
+ def method(cls, x):
+ print(cls, x)
+
+In []: A.method(1)
+<class '__main__.A'> 1
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Class methods}
+ \begin{itemize}
+ \item \py{@classmethod} is a special decorator
+ \item Makes the method a class method
+ \item Implicit argument is the class (and not the instance)
+ \item Useful when you don't want to construct the object
+ \end{itemize}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Summary}
+ \begin{itemize}
+ \item Class attributes
+ \item Class methods and the \py{@classmethod} decorator
+ \end{itemize}
+\end{frame}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: Point class attribute}
+ \begin{block}{}
+ Create a simple \py{Point} class with class attributes \py{x, y} that
+ default to 0.0.
+ \end{block}
+
+\begin{lstlisting}
+In []: p = Point()
+In []: p.x, p.y
+Out[]: (0.0, 0.0)
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Point:
+ x = 0.0
+ y = 0.0
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: classmethod}
+ \begin{block}{}
+ Create a simple \py{Point} class with instance attributes \py{x, y} but
+ add a class method \py{polar(r, theta)} that takes coordinates in $(r,
+ \theta)$ form but returns a suitable point. Remember that, $x=r
+ \cos(\theta), y=r\sin(\theta)$. For example:
+ \end{block}
+
+\begin{lstlisting}
+In []: p = Point.polar(1.0, 0.0)
+In []: p.x, p.y
+Out[]: (1.0, 0.0)
+\end{lstlisting}
+
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+from math import sin, cos
+class Point:
+ def __init__(self, x=0.0, y=0.0):
+ self.x = x
+ self.y = y
+ @classmethod
+ def polar(cls, r, theta):
+ x = r*cos(theta)
+ y = r*sin(theta)
+ return cls(x, y)
+
+\end{lstlisting}
+\end{frame}
+
+
+
+\end{document}
diff --git a/advanced_python/11_oop_misc.tex b/advanced_python/11_oop_misc.tex
new file mode 100644
index 0000000..d1f8aed
--- /dev/null
+++ b/advanced_python/11_oop_misc.tex
@@ -0,0 +1,154 @@
+\documentclass[14pt,compress,aspectratio=169]{beamer}
+
+\input{macros.tex}
+
+\title[OOP: Miscellaneous]{Advanced Python}
+\subtitle{Object Oriented Programming: miscellaneous}
+
+\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 Classes for encapsulation
+ \item Inheritance
+ \item \py{super,isinstance,issubclass}
+ \item Containership
+ \item Class attributes and methods
+ \vspace*{0.3in}
+ \item Look at some odds and ends
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{More on methods}
+\begin{lstlisting}
+class A:
+ def f(self):
+ print("A.f")
+
+In []: a = A()
+In []: a.f()
+A.f
+In []: A.f(a)
+
+In []: A.f()
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{More on methods}
+\begin{lstlisting}
+In []: type(a.f)
+Out[]: method
+In []: type(A.f)
+Out[]: function
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Private and public attributes}
+ \begin{itemize}
+ \item No explicit support
+ \item In practice, use \py{_attr} for private attributes/methods
+ \end{itemize}
+\begin{lstlisting}
+class A:
+ def __init__(self, x):
+ self._x = x
+ def _private_method(self):
+ pass
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Private and public attributes}
+ \begin{itemize}
+ \item Access private methods/attributes inside class
+ \item Avoid access from outside
+ \end{itemize}
+
+\begin{lstlisting}
+In []: a = A()
+In []: a._method() # Bad!
+In []: a._x # Bad!
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Private and public attributes}
+\begin{lstlisting}
+class B:
+ def _f(self):
+ # OK!
+ return self._x
+
+ def func(self):
+ # Also OK!
+ return self._f()
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Abstract classes}
+ \begin{itemize}
+ \item Used to specify an ``interface''
+ \item Not meant to instantiate but subclass
+ \item Useful to specify behavior
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Abstract classes}
+\begin{lstlisting}
+class Abstract:
+ def func(self):
+ raise NotImplementedError('override')
+
+class Derived(Abstract):
+ def func(self):
+ print("Hello world!")
+\end{lstlisting}
+ \begin{itemize}
+ \item \py{Derived} is a concrete class
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Modern abstract classes}
+\begin{lstlisting}
+from abc import ABC, abstractmethod
+
+class Abstract(ABC):
+ @abstractmethod
+ def func(self):
+ pass
+\end{lstlisting}
+ \begin{itemize}
+ \item More elegant and simple
+ \item Cannot instantiate \py{Abstract}
+ \end{itemize}
+\end{frame}
+
+
+\begin{frame}
+ \frametitle{Summary}
+ \begin{itemize}
+ \item Understanding methods a bit better
+ \item Private/public methods/attributes
+ \item Abstract and concrete classes
+ \item Using the \py{abc} module
+ \end{itemize}
+\end{frame}
+
+
+\end{document}
diff --git a/advanced_python/Makefile b/advanced_python/Makefile
index 5d0a0e4..14a8c9b 100644
--- a/advanced_python/Makefile
+++ b/advanced_python/Makefile
@@ -12,7 +12,9 @@ SLIDES= 1_intro.pdf \
6_practice_functions.pdf \
7_oop_basics.pdf \
8_oop_inheritance.pdf \
- 9_oop_containership.pdf
+ 9_oop_containership.pdf \
+ 10_oop_classattr.pdf \
+ 11_oop_misc.pdf
all: $(SLIDES)