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
|
\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}
|