-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPythonClasses.html
More file actions
438 lines (406 loc) · 38.5 KB
/
PythonClasses.html
File metadata and controls
438 lines (406 loc) · 38.5 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Python Classes — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="OO Intro - Report Class" href="../exercises/oo_intro/oo_intro.html" />
<link rel="prev" title="Object Orientation Overview" href="ObjectOrientationOverview.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="ObjectOrientationOverview.html">Object Orientation Overview</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Python Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/oo_intro/oo_intro.html">OO Intro - Report Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-oo.html">Mailroom - Object Oriented</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/13-intro_oo/index.html"><span class="section-number">13. </span>Intro to Object Oriented Programing</a> »</li>
<li>Python Classes</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/PythonClasses.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="ObjectOrientationOverview.html" class="btn btn-neutral float-left" title="Object Orientation Overview" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/oo_intro/oo_intro.html" class="btn btn-neutral float-right" title="OO Intro - Report Class" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="python-classes">
<span id="id1"></span><h1>Python Classes<a class="headerlink" href="#python-classes" title="Permalink to this headline"></a></h1>
<p>“Classes” are the core of Object Oriented Programming.</p>
<p>They provide the tools for encapsulation (keeping the data with the functions) and subclassing (making custom versions of certain types) and polymorphism (making different classes reusable in the same context).</p>
<div class="section" id="how-are-classes-made-in-python">
<h2>How are classes made in Python?<a class="headerlink" href="#how-are-classes-made-in-python" title="Permalink to this headline"></a></h2>
<div class="section" id="the-class-statement">
<h3>The <code class="docutils literal notranslate"><span class="pre">class</span></code> statement<a class="headerlink" href="#the-class-statement" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">class</span></code> statement creates a new type object:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="go"> pass</span>
<span class="gp"> ...:</span>
<span class="gp">In [5]: </span><span class="nb">type</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
<span class="gh">Out[5]: </span><span class="go">type</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>A class is a type – interesting! Remember how everything is an object in Python? well, every object is of a certain type. There are the built-in types that you are used to: integers, strings, lists, …. When you create a new class, you are creating a new type, and it is exactly the same as the built in ones.</p>
</div>
<p>The class is created when the <code class="docutils literal notranslate"><span class="pre">class</span></code> statement is run – much like <code class="docutils literal notranslate"><span class="pre">def</span></code> creates a function object.</p>
<p>So we now have a new type, or class – it doesn’t have any actual functionality yet, though by default all classes “inherit” from <code class="docutils literal notranslate"><span class="pre">object</span></code>. In doing so they get some minimal functionality from that:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="nb">issubclass</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="nb">object</span><span class="p">)</span>
<span class="gh">Out[3]: </span><span class="go">True</span>
</pre></div>
</div>
<p>We can print it:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="nb">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
<span class="go"><class '__main__.C'></span>
</pre></div>
</div>
<p>And look at all the methods it has!</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="nb">dir</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
<span class="gh">Out[5]:</span>
<span class="go">['__class__',</span>
<span class="go"> '__delattr__',</span>
<span class="go"> '__dict__',</span>
<span class="go"> '__dir__',</span>
<span class="go"> '__doc__',</span>
<span class="go"> '__eq__',</span>
<span class="go"> '__format__',</span>
<span class="go"> '__ge__',</span>
<span class="go"> '__getattribute__',</span>
<span class="go"> '__gt__',</span>
<span class="go"> '__hash__',</span>
<span class="go"> '__init__',</span>
<span class="go"> '__init_subclass__',</span>
<span class="go"> '__le__',</span>
<span class="go"> '__lt__',</span>
<span class="go"> '__module__',</span>
<span class="go"> '__ne__',</span>
<span class="go"> '__new__',</span>
<span class="go"> '__reduce__',</span>
<span class="go"> '__reduce_ex__',</span>
<span class="go"> '__repr__',</span>
<span class="go"> '__setattr__',</span>
<span class="go"> '__sizeof__',</span>
<span class="go"> '__str__',</span>
<span class="go"> '__subclasshook__',</span>
<span class="go"> '__weakref__']</span>
</pre></div>
</div>
<p>Most of those don’t do anything – but they are there, so every class is guaranteed to have all the “stuff” Python expects objects to have.</p>
<p>In order for the class to do anything useful, it needs to be given attributes and methods.</p>
</div>
<div class="section" id="a-simple-class">
<h3>A simple <code class="docutils literal notranslate"><span class="pre">class</span></code><a class="headerlink" href="#a-simple-class" title="Permalink to this headline"></a></h3>
<p>About the simplest class you can write that is still useful:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="n">Point</span>
<span class="go"><class __main__.Point at 0x2bf928></span>
<span class="gp">>>> </span><span class="n">Point</span><span class="o">.</span><span class="n">x</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">p</span>
<span class="go"><__main__.Point instance at 0x2de918></span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">x</span>
<span class="go">1</span>
</pre></div>
</div>
<p>This looks a lot like a “struct” in C – Python doesn’t have structures, so yes, a class with no methods (functions) is essentially a struct.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In practice, it is very common to use a simple class like this to store related data, even if there are no functions involved. So common, in fact, that in Python 3.8, a standard library package called <code class="docutils literal notranslate"><span class="pre">dataclasses</span></code> was added that generates classes for storing data like this – without having to write hardly any code. I encourage you to check it out for real use, but for now, we’ll build things from scratch so that you can learn how it all works.</p>
</div>
</div>
<div class="section" id="basic-structure-of-a-class">
<h3>Basic Structure of a class<a class="headerlink" href="#basic-structure-of-a-class" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
<span class="c1"># everything defined in here is in the class namespace</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
</pre></div>
</div>
<p>So this class has a method called “__init__” – which is a Python special method. Almost all classes have an <code class="docutils literal notranslate"><span class="pre">__init__</span></code> method</p>
<p>see: <a class="reference download internal" download="" href="../_downloads/6c3432a547b9c850475df13d5cde0080/simple_classes.py"><code class="xref download docutils literal notranslate"><span class="pre">simple_classes.py</span></code></a></p>
</div>
<div class="section" id="the-initializer">
<h3>The Initializer<a class="headerlink" href="#the-initializer" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">__init__</span></code> special method is known as the initializer. It is automatically called when a new instance of a class is created.</p>
<p>You can use it to do any set-up you need:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
</pre></div>
</div>
<p>It gets the arguments passed when you call the class object:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you have defined an __init__, you can create “instances” of the class:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p>And access the attributes:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"p.x is:"</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"p.y is:"</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="self">
<h3>Self<a class="headerlink" href="#self" title="Permalink to this headline"></a></h3>
<p>What is this <code class="docutils literal notranslate"><span class="pre">self</span></code> thing?</p>
<p>The instance of the class is passed as the first parameter for every method.</p>
<p>The name “<code class="docutils literal notranslate"><span class="pre">self</span></code>” is only a convention – but you <em>DO</em> want to use it.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">a_function</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>Does this look familiar from C-style procedural programming?</p>
<p>Anything assigned to a <code class="docutils literal notranslate"><span class="pre">self.</span></code> attribute is kept in the instance
name space – <code class="docutils literal notranslate"><span class="pre">self</span></code> <em>is</em> the instance.</p>
<p>That’s where all the instance-specific data is.</p>
</div>
<div class="section" id="class-attributes">
<h3>Class Attributes<a class="headerlink" href="#class-attributes" title="Permalink to this headline"></a></h3>
<p>In the above example, we assigned two attributes to <code class="docutils literal notranslate"><span class="pre">self</span></code> – these are going to be different for each instance, or copy of this class. But what if you want all the instances of a class to share the same values?</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">size</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">color</span><span class="o">=</span> <span class="s2">"red"</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
</pre></div>
</div>
<p>Anything assigned in the class scope is a class attribute – every
instance of the class shares the same one.</p>
<p>Note: the methods defined by <code class="docutils literal notranslate"><span class="pre">def</span></code> are class attributes as well.</p>
<p>The class is one namespace, the instance is another.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
<span class="n">size</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">color</span> <span class="o">=</span> <span class="s2">"red"</span>
<span class="o">...</span>
<span class="k">def</span> <span class="nf">get_color</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">color</span>
<span class="o">>>></span> <span class="n">p3</span><span class="o">.</span><span class="n">get_color</span><span class="p">()</span>
<span class="s1">'red'</span>
</pre></div>
</div>
<p>So in this case, <code class="docutils literal notranslate"><span class="pre">size</span></code> and <code class="docutils literal notranslate"><span class="pre">color</span></code> are class attributes.</p>
<p>But note in <code class="docutils literal notranslate"><span class="pre">get_color</span></code> – it accesses color from <code class="docutils literal notranslate"><span class="pre">self</span></code>:</p>
<p>class attributes are accessed with <code class="docutils literal notranslate"><span class="pre">self</span></code> also.</p>
<p>So what is the difference?</p>
<blockquote>
<div><ul class="simple">
<li><p>class attributes are shared by ALL the instances of the class.</p></li>
<li><p>instance attributes are unique to each instance – each one has its own copy.</p></li>
</ul>
</div></blockquote>
<p>Example:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="gp"> ...: </span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="c1"># class attribute</span>
<span class="gp"> ...: </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp"> ...: </span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span> <span class="c1"># instance attribute</span>
<span class="gp"> ...:</span>
<span class="gp">In [7]: </span><span class="n">c1</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">In [8]: </span><span class="n">c2</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">In [9]: </span><span class="n">c1</span><span class="o">.</span><span class="n">x</span> <span class="ow">is</span> <span class="n">c2</span><span class="o">.</span><span class="n">x</span> <span class="c1"># does each instance see the same x?</span>
<span class="gh">Out[9]: </span><span class="go">True</span>
<span class="gp">In [10]: </span><span class="n">c1</span><span class="o">.</span><span class="n">y</span> <span class="ow">is</span> <span class="n">c2</span><span class="o">.</span><span class="n">y</span> <span class="c1"># does each instance see the same y?</span>
<span class="gh">Out[10]: </span><span class="go">False</span>
</pre></div>
</div>
<p>But what are the consequences of this? It’s a <strong>very</strong> important distinction. watch what happens if we change something in these objects, adding a new item to both the lists in <code class="docutils literal notranslate"><span class="pre">c1</span></code>:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="go"># add an item to c1's x list</span>
<span class="gp">In [5]: </span><span class="n">c1</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="gp">In [6]: </span><span class="n">c1</span><span class="o">.</span><span class="n">x</span>
<span class="gh">Out[6]: </span><span class="go">[1, 2, 3, 100]</span>
<span class="gp">In [7]: </span><span class="n">c2</span><span class="o">.</span><span class="n">x</span>
<span class="gh">Out[7]: </span><span class="go">[1, 2, 3, 100]</span>
</pre></div>
</div>
<p>Note that adding something to <code class="docutils literal notranslate"><span class="pre">c1.x</span></code> also changed <code class="docutils literal notranslate"><span class="pre">c2.x</span></code> that is because they are the <em>same</em> list – <code class="docutils literal notranslate"><span class="pre">.x</span></code> is a <em>class attribute</em> – c1 and c2 share the same class, so they share the same class attributes.</p>
<p>But if we change <code class="docutils literal notranslate"><span class="pre">y</span></code>, an instance attribute:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">c1</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span>
<span class="gp">In [9]: </span><span class="n">c1</span><span class="o">.</span><span class="n">y</span>
<span class="gh">Out[9]: </span><span class="go">[4, 5, 6, 200]</span>
<span class="gp">In [10]: </span><span class="n">c2</span><span class="o">.</span><span class="n">y</span>
<span class="gh">Out[10]: </span><span class="go">[4, 5, 6]</span>
</pre></div>
</div>
<p>appending to <code class="docutils literal notranslate"><span class="pre">c1.y</span></code> did not change <code class="docutils literal notranslate"><span class="pre">c2.y</span></code> – <code class="docutils literal notranslate"><span class="pre">y</span></code> in this case is a an <em>instance</em> attribute – each instance has its own version – changing one will not affect the others.</p>
<p>So when you are deciding where to “put” something, you need to think about whether all instances need the same thing, or if they each need their own version of the attribute.</p>
<p>As a class attribute, you can access it from the class namespace as well, and it will affect all instances of that class:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">11</span><span class="p">]:</span> <span class="n">C</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">2222</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">12</span><span class="p">]:</span> <span class="n">c1</span><span class="o">.</span><span class="n">x</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">12</span><span class="p">]:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2222</span><span class="p">]</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">13</span><span class="p">]:</span> <span class="n">c2</span><span class="o">.</span><span class="n">x</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">13</span><span class="p">]:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2222</span><span class="p">]</span>
</pre></div>
</div>
<p>So here we changed <code class="docutils literal notranslate"><span class="pre">x</span></code> on the <em>class</em> object, <code class="docutils literal notranslate"><span class="pre">C</span></code>, and the change showed up in all the instances, <code class="docutils literal notranslate"><span class="pre">c1</span></code> and <code class="docutils literal notranslate"><span class="pre">c2</span></code>.</p>
<p>Instance attributes are far more common than class attributes. After all, the whole point of classes it to have instances with their own data.</p>
</div>
<div class="section" id="typical-methods">
<h3>Typical methods<a class="headerlink" href="#typical-methods" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">math</span>
<span class="k">class</span> <span class="nc">Circle</span><span class="p">:</span>
<span class="n">color</span> <span class="o">=</span> <span class="s2">"red"</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">diameter</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">=</span> <span class="n">diameter</span>
<span class="k">def</span> <span class="nf">expand</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">factor</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">*</span> <span class="n">factor</span>
<span class="k">return</span> <span class="kc">None</span> <span class="c1"># note that if you leave that off, it will still return None</span>
<span class="k">def</span> <span class="nf">area</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">area</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span>
<span class="k">return</span> <span class="n">area</span>
</pre></div>
</div>
<p>Methods take some parameters, and possibly manipulate the attributes in <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p>
<p>Remember that classes are about encapsulating the data and the functions that act on that data – the methods are the functions that act on the data.</p>
<p>They may or may not return something useful.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is a convention in Python that methods that change the internal state of an object return <code class="docutils literal notranslate"><span class="pre">None</span></code>, whereas methods that return a new object, or some calculated result without changing the state return that value.</p>
<p>You can see examples of this in the python built ins – methods of lists like <code class="docutils literal notranslate"><span class="pre">append</span></code> or <code class="docutils literal notranslate"><span class="pre">sort</span></code> return None – indicating that they have mutated the instance.</p>
</div>
</div>
<div class="section" id="gotcha">
<h3>Gotcha !<a class="headerlink" href="#gotcha" title="Permalink to this headline"></a></h3>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
<span class="k">def</span> <span class="nf">grow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">factor</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">diameter</span> <span class="o">*</span> <span class="n">factor</span>
<span class="o">...</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">205</span><span class="p">]:</span> <span class="n">C</span> <span class="o">=</span> <span class="n">Circle</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">206</span><span class="p">]:</span> <span class="n">C</span><span class="o">.</span><span class="n">grow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="n">grow</span><span class="p">()</span> <span class="n">takes</span> <span class="n">at</span> <span class="n">most</span> <span class="mi">2</span> <span class="n">arguments</span> <span class="p">(</span><span class="mi">3</span> <span class="n">given</span><span class="p">)</span>
</pre></div>
</div>
<p>Huh???? I only gave two arguments!</p>
<p><code class="docutils literal notranslate"><span class="pre">self</span></code> is implicitly passed in for you by Python. so it actually <em>did</em> get three!</p>
</div>
<div class="section" id="functions-methods-are-first-class-objects">
<h3>Functions (methods) are First Class Objects<a class="headerlink" href="#functions-methods-are-first-class-objects" title="Permalink to this headline"></a></h3>
<p>Note that in Python, functions are first class objects, so a method <em>is</em> an attribute.</p>
<p>All the same rules apply about attribute access: note that the methods are defined in the class – so they are class attributes.</p>
<p>All the instances share the same methods – there is only one copy of each method.</p>
<p>But each method gets its own namespace when it is actually called, so there is no confusion – just like when you call a regular function multiple times.</p>
</div>
<div class="section" id="manipulating-attributes">
<h3>Manipulating Attributes<a class="headerlink" href="#manipulating-attributes" title="Permalink to this headline"></a></h3>
<p>Python makes it very easy to manipulate object’s attributes – you can access them with the “dot” notation, and simply set them like any other variable. With the Circle class above:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">15</span><span class="p">]:</span> <span class="n">c</span> <span class="o">=</span> <span class="n">Circle</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">16</span><span class="p">]:</span> <span class="n">c</span><span class="o">.</span><span class="n">area</span><span class="p">()</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">16</span><span class="p">]:</span> <span class="mf">3.141592653589793</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">17</span><span class="p">]:</span> <span class="n">c</span><span class="o">.</span><span class="n">diameter</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">18</span><span class="p">]:</span> <span class="n">c</span><span class="o">.</span><span class="n">area</span><span class="p">()</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">18</span><span class="p">]:</span> <span class="mf">12.566370614359172</span>
</pre></div>
</div>
<p>Note that after I changed the diameter attribute, when I called the <code class="docutils literal notranslate"><span class="pre">area()</span></code> method it used the new diameter. Simple attribute access changed the state of the object.</p>
<p>So you now know how to:</p>
<blockquote>
<div><ul class="simple">
<li><p>Define a class</p></li>
<li><p>Give the class shared (class) attributes</p></li>
<li><p>Add an initializer to set up its initial state</p></li>
<li><p>Add methods to manipulate that state.</p></li>
<li><p>Add methods that return the results of calculations of the current state</p></li>
</ul>
</div></blockquote>
<p>You can do a lot with this simple functionality. Frankly, all creating classes like this has done is put everything together in a neat package – which is very useful, but hasn’t given you much new power.</p>
<p>But it’s a good idea to get the hang of using classes, and methods, and <code class="docutils literal notranslate"><span class="pre">self</span></code> for a bit before moving on to the more powerful feature of subclassing.</p>
</div>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="ObjectOrientationOverview.html" class="btn btn-neutral float-left" title="Object Orientation Overview" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/oo_intro/oo_intro.html" class="btn btn-neutral float-right" title="OO Intro - Report Class" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>