forked from gc3-uzh-ch/python-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part04.tex
276 lines (215 loc) · 5.5 KB
/
part04.tex
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
\documentclass[english,serif,mathserif,xcolor=pdftex,dvipsnames,table]{beamer}
\usetheme[informal]{s3it}
\usepackage{s3it}
\title[Objects]{%
Everything is an object!
}
\author[S3IT]{%
S3IT: Services and Support for Science IT, \\
University of Zurich
}
\date{June~23--24, 2014}
\begin{document}
% title frame
\maketitle
\begin{frame}
\frametitle{What's an \emph{object}?}
\textbf{A Python object is a bundle of variables and functions.}
\+
What variable names and functions comprise an object is defined
by the object's \emph{class}.
\+
From one class specification, many objects can be
\emph{instanciated}. Different instances can assign different
values to the object variables.
\+
Variables and functions in an instance are collectively called
\emph{instance attributes}; functions are also termed \emph{instance
methods}.
\end{frame}
\begin{frame}[fragile]
\frametitle{Example: the \texttt{datetime} object, I}
\begin{columns}[c]
\begin{column}{0.5\textwidth}
\begin{lstlisting}
>>> ~\HL{from datetime import date}~
>>> dt1 = date(2012, 9, 28)
>>> dt2 = date(2012, 10, 1)
\end{lstlisting}
\end{column}
\begin{column}{0.45\textwidth}
\raggedleft
Import the \texttt{date} class from the standard
library module \texttt{datetime}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Example: the \texttt{datetime} object, II}
\begin{columns}[c]
\begin{column}{0.5\textwidth}
\begin{lstlisting}
>>> from datetime import date
>>> ~\HL{dt1 = date(2012, 9, 28)}~
>>> dt2 = date(2012, 10, 1)
\end{lstlisting}
\end{column}
\begin{column}{0.5\textwidth}
\raggedleft
To instanciate an object, call the class name like a
function.
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Example: the \texttt{datetime} object, III}
\begin{lstlisting}
>>> dir(dt1)
['__add__', '__class__', ~\ldots~, 'ctime', 'day',
'fromordinal', 'fromtimestamp', 'isocalendar',
'isoformat', 'isoweekday', 'max', 'min', 'month',
'replace', 'resolution', 'strftime', 'timetuple',
'today', 'toordinal', 'weekday', 'year']
\end{lstlisting}
\+
The \texttt{dir} function can list all objects attributes.
\+
Note there is no distinction between instance variables and
methods!
\end{frame}
\begin{frame}[fragile]
\frametitle{Example: the \texttt{datetime} object, IV}
\begin{columns}[c]
\begin{column}{0.5\textwidth}
\begin{lstlisting}
>>> dt1.day
28
>>> dt1.month
9
>>> dt1.year
2012
\end{lstlisting}
\end{column}
\begin{column}{0.5\textwidth}
\raggedleft
Access to object attributes is done by suffixing the
instance name with the attribute name, separated by a dot
``\texttt{.}''.
\end{column}
\end{columns}
\end{frame}
\begin{frame}
\frametitle{Objects \emph{vs} modules}
Modules are also namespaces of variables and functions.
\+
The dot operator `\texttt{.}' is also used to access variables
and functions from modules. The \texttt{dir()} function is also
used to list variables and functions from modules.
\+
But each module has \emph{one and only one} instance in a Python
program.
\end{frame}
\begin{frame}[fragile]
\frametitle{Example: the \texttt{datetime} object, V}
\begin{columns}[c]
\begin{column}[b]{0.5\textwidth}
\begin{lstlisting}
>>> dt1 = date(2012, 9, 28)
>>> dt2 = date(2012, 10, 1)
>>> dt1.day
~\HL{28}~
>>> dt2.day
~\HL{1}~
\end{lstlisting}
\end{column}
\begin{column}[b]{0.5\textwidth}
\raggedleft
The same attribute can have different
values in different instances!
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Instance methods}
\begin{lstlisting}[showstringspaces=false]
>>> dt1.isoformat()
'2012-09-28'
\end{lstlisting}
\+
Invoke an instance method just like any other function.
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
The \texttt{dir} built-in function is used to list the attributes of an object.
\begin{python}
>>> dir("hello!")
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
The \texttt{dir} built-in function is used to list the attributes of an object.
\begin{python}
>>> ~\HL{dir("hello!")}~
['__add__', '__class__', '__contains__',
'__delattr__', '__doc__', '__eq__',
~\ldots~
'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
\end{python}
\+\ldots a string is an object!
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
\begin{python}
>>> ~\HL{dir([1,2,3])}~
['__add__', '__class__', '__contains__',
~\ldots~
'append', 'count', 'extend',
'index', 'insert', 'pop',
'remove', 'reverse', 'sort']
\end{python}
\+\ldots a \texttt{list} is an object!
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
Indeed, you can do:
\+
\begin{python}
>>> "hello world!".split()
['hello', 'world!']
\end{python}
\+
\begin{python}
>>> [1,1,2,3,5].count(1)
2
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
\begin{python}
>>> ~\HL{dir(1)}~
\end{python}
\end{frame}
\begin{frame}[fragile]
\frametitle{Everything is an object!}
\begin{python}
>>> ~\HL{dir(1)}~
['__abs__', '__add__', '__and__',
~\ldots~
'conjugate', 'denominator',
'imag', 'numerator', 'real']
\end{python}
\+\ldots an \texttt{int} is an object!
\+
\begin{python}
>>> (1).numerator
2
>>> (1).denominator
1
\end{python}
\end{frame}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: