blob: d1f8aed2e931f942e7f86bcb81c82985cf217eaa (
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
|
\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}
|