- Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathClassDefinition.html
325 lines (290 loc) · 26.7 KB
/
ClassDefinition.html
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
<!DOCTYPE html>
<htmlclass="writer-html5" lang="en" >
<head>
<metacharset="utf-8" /><metacontent="Topic: The rules for defining classes in python, Difficulty: Easy, Category: Section" name="description" />
<metacontent="class definition, scope, class object, attribute, method" name="keywords" />
<metaname="viewport" content="width=device-width, initial-scale=1.0" />
<title>Defining a New Class of Object — Python Like You Mean It</title>
<linkrel="stylesheet" href="../_static/pygments.css" type="text/css" />
<linkrel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<linkrel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<scriptdata-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<scriptsrc="../_static/jquery.js"></script>
<scriptsrc="../_static/underscore.js"></script>
<scriptsrc="../_static/doctools.js"></script>
<scriptasync="async" src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<scriptsrc="../_static/gtag.js"></script>
<scriptcrossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script>window.MathJax={"tex": {"inlineMath": [["$","$"],["\\(","\\)"]],"processEscapes": true},"options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document","processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<scriptdefer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<scriptsrc="../_static/js/theme.js"></script>
<linkrel="index" title="Index" href="../genindex.html" />
<linkrel="search" title="Search" href="../search.html" />
<linkrel="next" title="Instances of a Class" href="ClassInstances.html" />
<linkrel="prev" title="Introduction to Object Oriented Programming" href="Introduction_to_OOP.html" />
</head>
<bodyclass="wy-body-for-nav">
<divclass="wy-grid-for-nav">
<navdata-toggle="wy-nav-shift" class="wy-nav-side">
<divclass="wy-side-scroll">
<divclass="wy-side-nav-search" >
<ahref="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<divclass="version">
1.4
</div>
<divrole="search">
<formid="rtd-search-form" class="wy-form" action="../search.html" method="get">
<inputtype="text" name="q" placeholder="Search docs" />
<inputtype="hidden" name="check_keywords" value="yes" />
<inputtype="hidden" name="area" value="default" />
</form>
</div>
</div><divclass="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<pclass="caption" role="heading"><spanclass="caption-text">Table of Contents:</span></p>
<ulclass="current">
<liclass="toctree-l1"><aclass="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_2_problems.html">Module 2: Problems</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_3_problems.html">Module 3: Problems</a></li>
<liclass="toctree-l1 current"><aclass="reference internal" href="../module_4.html">Module 4: Object Oriented Programming</a><ulclass="current">
<liclass="toctree-l2"><aclass="reference internal" href="Introduction_to_OOP.html">Introduction to Object Oriented Programming</a></li>
<liclass="toctree-l2 current"><aclass="current reference internal" href="#">Defining a New Class of Object</a><ul>
<liclass="toctree-l3"><aclass="reference internal" href="#The-General-Form-of-a-Class-Definition">The General Form of a Class Definition</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Working-with-Object-Attributes">Working with Object Attributes</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Links-to-Official-Documentation">Links to Official Documentation</a></li>
<liclass="toctree-l3"><aclass="reference internal" href="#Reading-Comprehension-Solutions">Reading Comprehension Solutions</a></li>
</ul>
</li>
<liclass="toctree-l2"><aclass="reference internal" href="ClassInstances.html">Instances of a Class</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Brief_Review.html">A Brief Summary of Terms and Concepts</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Methods.html">Methods</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Applications_of_OOP.html">Applications of Object Oriented Programming</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Special_Methods.html">Special Methods</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="Inheritance.html">Inheritance</a></li>
</ul>
</li>
<liclass="toctree-l1"><aclass="reference internal" href="../module_5.html">Module 5: Odds and Ends</a></li>
<liclass="toctree-l1"><aclass="reference internal" href="../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<sectiondata-toggle="wy-nav-shift" class="wy-nav-content-wrap"><navclass="wy-nav-top" aria-label="Mobile navigation menu" >
<idata-toggle="wy-nav-top" class="fa fa-bars"></i>
<ahref="../index.html">Python Like You Mean It</a>
</nav>
<divclass="wy-nav-content">
<divclass="rst-content">
<divrole="navigation" aria-label="Page navigation">
<ulclass="wy-breadcrumbs">
<li><ahref="../index.html" class="icon icon-home"></a> »</li>
<li><ahref="../module_4.html">Module 4: Object Oriented Programming</a> »</li>
<li>Defining a New Class of Object</li>
<liclass="wy-breadcrumbs-aside">
<ahref="../_sources/Module4_OOP/ClassDefinition.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<divrole="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<divitemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom:19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container+ .nbinput.container {
margin-top:-19px;
}
.admonition>p:before {
margin-right:4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<divclass="section" id="Defining-a-New-Class-of-Object">
<h1>Defining a New Class of Object<aclass="headerlink" href="#Defining-a-New-Class-of-Object" title="Permalink to this headline"></a></h1>
<p>This section will introduce the basic syntax for defining a new class (a.k.a. type) of Python object. Recall that the statement <codeclass="docutils literal notranslate"><spanclass="pre">def</span></code> is used to denote the definition of a function. Similarly, <codeclass="docutils literal notranslate"><spanclass="pre">class</span></code> is used to denote the beginning of a class definition. The body of the class definition, which is the indented region below a <codeclass="docutils literal notranslate"><spanclass="pre">class</span></code> statement, is used to define the class’ various <strong>attributes</strong>.</p>
<p>The following defines a new class of object, named <codeclass="docutils literal notranslate"><spanclass="pre">MyGuy</span></code>, specifying four attributes <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">y</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">z</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">f</span></code></p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># defining a new class/type of object</span>
<spanclass="k">class</span><spanclass="nc">MyGuy</span><spanclass="p">:</span>
<spanclass="n">x</span><spanclass="o">=</span><spanclass="mi">1</span><spanclass="o">+</span><spanclass="mi">2</span>
<spanclass="n">y</span><spanclass="o">=</span><spanclass="p">[</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="mi">6</span><spanclass="p">]</span>
<spanclass="n">z</span><spanclass="o">=</span><spanclass="s2">"hi"</span>
<spanclass="k">def</span><spanclass="nf">f</span><spanclass="p">():</span>
<spanclass="k">return</span><spanclass="mi">3</span>
<spanclass="c1"># leaving the indented region ends the class definition</span>
</pre></div>
</div>
<p>Once this definition for a new class of object is executed, you can proceed to reference that object in your code. Here, we will access the various attributes of <codeclass="docutils literal notranslate"><spanclass="pre">MyGuy</span></code>.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">x</span>
<spanclass="go">3</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">y</span>
<spanclass="go">[2, 4, 6]</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">z</span>
<spanclass="go">"hi"</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">f</span>
<spanclass="go"><function __main__.MyGuy.f></span>
</pre></div>
</div>
<p>See that all of the attributes can be accessed using the “dot” syntax: <codeclass="docutils literal notranslate"><spanclass="pre">object.attribute_name</span></code>. The attribute <codeclass="docutils literal notranslate"><spanclass="pre">f</span></code> is a function, thus we can call it and it will evaluate as expected:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># calling the attribute f</span>
<spanclass="o">>>></span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">f</span><spanclass="p">()</span>
<spanclass="mi">3</span>
</pre></div>
</div>
<p>An object attribute that is also a function is referred to as a <strong>method</strong>. Thus <codeclass="docutils literal notranslate"><spanclass="pre">f</span></code> is a method of <codeclass="docutils literal notranslate"><spanclass="pre">MyGuy</span></code>.</p>
<p><codeclass="docutils literal notranslate"><spanclass="pre">MyGuy</span></code> is the singular class object that embodies our class definition. It is akin to <codeclass="docutils literal notranslate"><spanclass="pre">list</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">str</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">int</span></code>. We will use <codeclass="docutils literal notranslate"><spanclass="pre">MyGuy</span></code> to create objects that are <em>instances</em> of our class, in the same way that <codeclass="docutils literal notranslate"><spanclass="pre">"cat"</span></code> is an instance of <codeclass="docutils literal notranslate"><spanclass="pre">str</span></code>. More on this soon.</p>
<divclass="admonition note">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Takeaway:</strong></p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">class</span></code> expression denotes the definition of a new class of object, which entails defining the attributes of that class. An attribute can “bind” to that class other Python objects (integers, strings, lists, etc), including functions. Attributes that are functions are called <em>methods</em>. The syntax <codeclass="docutils literal notranslate"><spanclass="pre">obj.attr</span></code> is the dot syntax for “getting” the attribute named <codeclass="docutils literal notranslate"><spanclass="pre">attr</span></code> from the object named <codeclass="docutils literal notranslate"><spanclass="pre">obj</span></code>.</p>
</div>
<divclass="section" id="The-General-Form-of-a-Class-Definition">
<h2>The General Form of a Class Definition<aclass="headerlink" href="#The-General-Form-of-a-Class-Definition" title="Permalink to this headline"></a></h2>
<p>The general form for a class definition is simply a collection of attribute definitions, which either take the form of variable assignments or function definitions, resulting in the formation of a new class of object, with its attributes and methods:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="k">class</span><spanclass="nc">ClassName</span><spanclass="p">:</span>
<spanclass="sd">""" class docstring """</span>
<spanclass="o"><</span><spanclass="n">statement</span><spanclass="o">-</span><spanclass="mi">1</span><spanclass="o">></span>
<spanclass="o">.</span>
<spanclass="o">.</span>
<spanclass="o">.</span>
<spanclass="o"><</span><spanclass="n">statement</span><spanclass="o">-</span><spanclass="n">N</span><spanclass="o">></span>
</pre></div>
</div>
<p>where each <codeclass="docutils literal notranslate"><spanclass="pre"><statement-j></span></code> defines an attribute (e.g. <codeclass="docutils literal notranslate"><spanclass="pre">z</span><spanclass="pre">=</span><spanclass="pre">"hi"</span></code> defines the attribute <codeclass="docutils literal notranslate"><spanclass="pre">z</span></code>, or a function definition creates a method) for that class of object.</p>
<p>Similar to function definitions, class definitions can contain effectively arbitrary Python code, and the definition has its own <aclass="reference external" href="http://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Scope.html">scope</a>; however, <em>any</em> variables assigned within the class definition will be available as attributes.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Any variable assigned within a class definition becomes</span>
<spanclass="c1"># available as an attribute for that class of object, even</span>
<spanclass="c1"># a variable defined in a for-loop becomes an attribute of</span>
<spanclass="c1"># that class.</span>
<spanclass="k">class</span><spanclass="nc">Dummy</span><spanclass="p">:</span>
<spanclass="n">cnt</span><spanclass="o">=</span><spanclass="mi">0</span>
<spanclass="k">for</span><spanclass="n">i</span><spanclass="ow">in</span><spanclass="nb">range</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">11</span><spanclass="p">):</span>
<spanclass="c1"># i = 5</span>
<spanclass="c1"># i = 6</span>
<spanclass="c1"># ...</span>
<spanclass="c1"># i = 10</span>
<spanclass="n">cnt</span><spanclass="o">+=</span><spanclass="n">i</span>
<spanclass="c1"># last iteration of loop assigns i = 10</span>
<spanclass="c1"># thus i is an attribute of Dummy with value 10</span>
</pre></div>
</div>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>> </span><spanclass="n">Dummy</span><spanclass="o">.</span><spanclass="n">cnt</span><spanclass="c1"># cnt = 0 + 5 + 6 + 7 + 8 + 9 + 10</span>
<spanclass="go">45</span>
<spanclass="gp">>>> </span><spanclass="n">Dummy</span><spanclass="o">.</span><spanclass="n">i</span>
<spanclass="go">10</span>
</pre></div>
</div>
<divclass="admonition warning">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Naming Classes of Objects:</strong></p>
<p>The convention for naming a new class/type of object is to use “camel-casing”. Thus if I wanted to call my class of objects “pizza shop”, I would use the name <codeclass="docutils literal notranslate"><spanclass="pre">PizzaShop</span></code>. This is in contrast to variable names, function names, and <em>instances</em> of a class object (still to be introduced), where convention dictates the use of lower-case letters and underscores in place of spaces (snake-case).</p>
</div>
<divclass="admonition note">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Create Your Own Class of Object</strong></p>
<p>Create a definition for the class of object named <codeclass="docutils literal notranslate"><spanclass="pre">Dog</span></code>. This class should have two attributes: “name” and “speak”. The “name” attribute should bind a string to the object (the name of the dog). The “speak” attribute should be a <em>method</em>, that takes a string as an input argument and returns that string with <codeclass="docutils literal notranslate"><spanclass="pre">"*woof*"</span></code> added to either end of it (e.g. <codeclass="docutils literal notranslate"><spanclass="pre">"hello"</span></code> -> <codeclass="docutils literal notranslate"><spanclass="pre">"*woof*</span><spanclass="pre">hello</span><spanclass="pre">*woof*"</span></code>)</p>
</div>
</div>
<divclass="section" id="Working-with-Object-Attributes">
<h2>Working with Object Attributes<aclass="headerlink" href="#Working-with-Object-Attributes" title="Permalink to this headline"></a></h2>
<p>Attempting to access an undefined attribute from an object will raise an <codeclass="docutils literal notranslate"><spanclass="pre">AttributeError</span></code>:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">apple</span>
<spanclass="go">AttributeError: type object 'MyGuy' has no attribute 'apple'</span>
</pre></div>
</div>
<p>We can use built-in function <codeclass="docutils literal notranslate"><spanclass="pre">hasattr</span></code> to inspect if an object possesses a particular attribute:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># demonstrating `hasattr`</span>
<spanclass="o">>>></span><spanclass="nb">hasattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"apple"</span><spanclass="p">)</span><spanclass="c1"># MyGuy.apple is not defined</span>
<spanclass="kc">False</span>
<spanclass="o">>>></span><spanclass="nb">hasattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"x"</span><spanclass="p">)</span><spanclass="c1"># MyGuy.x is defined</span>
<spanclass="kc">True</span>
</pre></div>
</div>
<p>In addition to using the dot-syntax for accessing attributes, the built-in function <codeclass="docutils literal notranslate"><spanclass="pre">getattr</span></code> can be used to the same effect:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># demonstrating `getattr`</span>
<spanclass="o">>>></span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">x</span>
<spanclass="mi">3</span>
<spanclass="o">>>></span><spanclass="nb">getattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"x"</span><spanclass="p">)</span>
<spanclass="mi">3</span>
</pre></div>
</div>
<p>It may be surprising to discover that new attributes can be bound (or “set”) to the object <em>after</em> that class of object has already been defined. This can be done using the builtin-function <codeclass="docutils literal notranslate"><spanclass="pre">setattr</span></code>:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># use `setattr` to bind the attribute `apple` to `MyGuy`</span>
<spanclass="o">>>></span><spanclass="nb">hasattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"apple"</span><spanclass="p">)</span><spanclass="c1"># MyGuy.apple is not defined</span>
<spanclass="kc">False</span>
<spanclass="o">>>></span><spanclass="nb">setattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"apple"</span><spanclass="p">,</span><spanclass="s2">"red"</span><spanclass="p">)</span>
<spanclass="o">>>></span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">apple</span>
<spanclass="s1">'red'</span>
</pre></div>
</div>
<p>Attributes can be defined/set even less formally, using a simple assignment syntax:</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>> </span><spanclass="nb">hasattr</span><spanclass="p">(</span><spanclass="n">MyGuy</span><spanclass="p">,</span><spanclass="s2">"grape"</span><spanclass="p">)</span><spanclass="c1"># MyGuy.grape is not defined</span>
<spanclass="go">False</span>
<spanclass="go"># set the attribute `grape` to `MyGuy`</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">grape</span><spanclass="o">=</span><spanclass="s2">"purple"</span><spanclass="c1"># define and set the attribute 'grape'</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">grape</span>
<spanclass="go">'purple'</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">x</span><spanclass="o">=</span><spanclass="o">-</span><spanclass="mi">1</span><spanclass="c1"># set the attribute 'x' with a new value</span>
<spanclass="gp">>>> </span><spanclass="n">MyGuy</span><spanclass="o">.</span><spanclass="n">x</span>
<spanclass="go">-1</span>
</pre></div>
</div>
<p>It may seem like the class definition is reduced to a mere formality, since attributes can be set to an object at so casually. Although Python is known for permitting this loosey-goosey style of coding, know that it is generally bad form to create attributes for a class of object outside of its designated definition.</p>
<divclass="admonition note">
<pclass="admonition-title fa fa-exclamation-circle"><strong>Takeaway:</strong></p>
<p><codeclass="docutils literal notranslate"><spanclass="pre">hasattr</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">getattr</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">setattr</span></code> are built-in functions that allow us to, by the name of an attribute, check to see if it exists, access its value, and set its value, respectively. Python’s objects are shockingly flexible in that their attributes can be created outside of the formal space of the class definition. That being said, we should be civilized and treat the class definition as a formal contract/specification whenever possible.</p>
</div>
</div>
<divclass="section" id="Links-to-Official-Documentation">
<h2>Links to Official Documentation<aclass="headerlink" href="#Links-to-Official-Documentation" title="Permalink to this headline"></a></h2>
<ulclass="simple">
<li><p><aclass="reference external" href="https://docs.python.org/3/tutorial/classes.html#class-objects">Python Tutorial: Class Objects</a></p></li>
</ul>
</div>
<divclass="section" id="Reading-Comprehension-Solutions">
<h2>Reading Comprehension Solutions<aclass="headerlink" href="#Reading-Comprehension-Solutions" title="Permalink to this headline"></a></h2>
<p><strong>Set Creation: Solution</strong></p>
<p>Create a definition for the class of object named <codeclass="docutils literal notranslate"><spanclass="pre">Dog</span></code>. This class should have two attributes: “name” and “speak”. The “name” attribute should bind a string to the object (the name of the dog). The “speak” attribute should be a <em>method</em>, that takes a string as an input argument and returns that string with <codeclass="docutils literal notranslate"><spanclass="pre">"*woof*"</span></code> added to either end of it (e.g. <codeclass="docutils literal notranslate"><spanclass="pre">"hello"</span></code> -> <codeclass="docutils literal notranslate"><spanclass="pre">"*woof*</span><spanclass="pre">hello</span><spanclass="pre">*woof*"</span></code>)</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="k">class</span><spanclass="nc">Dog</span><spanclass="p">:</span>
<spanclass="n">name</span><spanclass="o">=</span><spanclass="s2">"Charlie"</span>
<spanclass="k">def</span><spanclass="nf">speak</span><spanclass="p">(</span><spanclass="n">input_string</span><spanclass="p">):</span>
<spanclass="k">return</span><spanclass="s2">"*woof* "</span><spanclass="o">+</span><spanclass="n">input_string</span><spanclass="o">+</span><spanclass="s2">" *woof*"</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<footer><divclass="rst-footer-buttons" role="navigation" aria-label="Footer">
<ahref="Introduction_to_OOP.html" class="btn btn-neutral float-left" title="Introduction to Object Oriented Programming" accesskey="p" rel="prev"><spanclass="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<ahref="ClassInstances.html" class="btn btn-neutral float-right" title="Instances of a Class" accesskey="n" rel="next">Next <spanclass="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<divrole="contentinfo">
<p>© Copyright 2021, Ryan Soklaski.</p>
</div>
Built with <ahref="https://www.sphinx-doc.org/">Sphinx</a> using a
<ahref="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <ahref="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function(){
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>