forked from UWPCE-PythonCert/IntroToPython-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession06.html
More file actions
745 lines (662 loc) · 47.2 KB
/
session06.html
File metadata and controls
745 lines (662 loc) · 47.2 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Session Six: Object oriented programming: Classes, instances, attributes, and subclassing — Introduction To Python 1.3 documentation</title>
<link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="top" title="Introduction To Python 1.3 documentation" href="index.html"/>
<link rel="next" title="Session Seven: Testing, More OO" href="session07.html"/>
<link rel="prev" title="Session Five: Advanced Argument passing, List and Dict Comprehensions, Lambda and Functional programming" href="session05.html"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="index.html" class="fa fa-home"> Introduction To Python</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="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="session01.html">Session One: Introductions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session01.html#introductions">Introductions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#introduction-to-this-class">Introduction to This Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#introduction-to-your-environment">Introduction to Your Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#setting-up-your-environment">Setting Up Your Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#introduction-to-ipython">Introduction to iPython</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#basic-python-syntax">Basic Python Syntax</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#id2">Homework</a></li>
<li class="toctree-l2"><a class="reference internal" href="session01.html#next-class">Next Class</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session02.html">Session Two: Functions, Booleans and Modules</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session02.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#git-work">Git Work</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#quick-intro-to-basics">Quick Intro to Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#functions">Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#in-class-lab">In-Class Lab:</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#boolean-expressions">Boolean Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#id1">In-Class Lab:</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#code-structure-modules-and-namespaces">Code Structure, Modules, and Namespaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#id4">In-Class Lab</a></li>
<li class="toctree-l2"><a class="reference internal" href="session02.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session03.html">Session Three: Sequences, Iteration and String Formatting</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session03.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#sequences">Sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#lists-tuples">Lists, Tuples...</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#mutability">Mutability</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#mutable-sequence-methods">Mutable Sequence Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#id1">Iteration</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#string-features">String Features</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#one-last-trick">One Last Trick</a></li>
<li class="toctree-l2"><a class="reference internal" href="session03.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session04.html">Session Four: Dictionaries, Sets, Exceptions, and Files</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session04.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#a-little-warm-up">A little warm up</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#dictionaries-and-sets">Dictionaries and Sets</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#exceptions">Exceptions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#file-reading-and-writing">File Reading and Writing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#paths-and-directories">Paths and Directories</a></li>
<li class="toctree-l2"><a class="reference internal" href="session04.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session05.html">Session Five: Advanced Argument passing, List and Dict Comprehensions, Lambda and Functional programming</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session05.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#advanced-argument-passing">Advanced Argument Passing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#a-bit-more-on-mutability-and-copies">A bit more on mutability (and copies)</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#list-and-dict-comprehensions">List and Dict Comprehensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#anonymous-functions">Anonymous functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#functional-programming">Functional Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="session05.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Session Six: Object oriented programming: Classes, instances, attributes, and subclassing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#object-oriented-programming">Object Oriented Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="#python-classes">Python Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="#subclassing-inheritance">Subclassing/Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="#more-on-subclassing">More on Subclassing</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session07.html">Session Seven: Testing, More OO</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session07.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#testing">Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#more-on-subclassing">More on Subclassing</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#properties">Properties</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#static-and-class-methods">Static and Class Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#special-methods">Special Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="session07.html#homework">Homework</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="session08.html">Session Eight: Generators, Iterators, Decorators, and Context Managers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="session08.html#review-questions">Review/Questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#decorators">Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#iterators-and-generators">Iterators and Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#context-managers">Context Managers</a></li>
<li class="toctree-l2"><a class="reference internal" href="session08.html#homework">Homework</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="homework/index.html">Homework Materials</a><ul>
<li class="toctree-l2"><a class="reference internal" href="homework/kata_fourteen.html">Kata Fourteen: Tom Swift Under Milk Wood</a></li>
<li class="toctree-l2"><a class="reference internal" href="homework/html_builder.html">HTML Renderer Homework Assignment</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="supplements/index.html">Supplemental Materials</a><ul>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_learning_resources.html">Useful Python Learning Resources</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_mac.html">Setting up your Mac for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_windows.html">Setting up Windows for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/python_for_linux.html">Setting up Linux for Python and this class</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/virtualenv.html">Working with Virtualenv</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/sublime_as_ide.html">Turning Sublime Text Into a Lightweight Python IDE</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/shell.html">Shell Customizations for Python Development</a></li>
<li class="toctree-l2"><a class="reference internal" href="supplements/unicode.html">Unicode in Python 2</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Introduction To Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Session Six: Object oriented programming: Classes, instances, attributes, and subclassing</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/session06.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main">
<div class="section" id="session-six-object-oriented-programming-classes-instances-attributes-and-subclassing">
<h1>Session Six: Object oriented programming: Classes, instances, attributes, and subclassing<a class="headerlink" href="#session-six-object-oriented-programming-classes-instances-attributes-and-subclassing" title="Permalink to this headline">¶</a></h1>
<div class="section" id="review-questions">
<h2>Review/Questions<a class="headerlink" href="#review-questions" title="Permalink to this headline">¶</a></h2>
<div class="section" id="review-of-previous-class">
<h3>Review of Previous Class<a class="headerlink" href="#review-of-previous-class" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Argument Passing: <tt class="docutils literal"><span class="pre">*args</span></tt>, <tt class="docutils literal"><span class="pre">**kwargs</span></tt></li>
<li>comprehensions</li>
<li><tt class="docutils literal"><span class="pre">lambda</span></tt></li>
</ul>
</div>
<div class="section" id="homework-review">
<h3>Homework review<a class="headerlink" href="#homework-review" title="Permalink to this headline">¶</a></h3>
<p>Homework Questions?</p>
<p>If it seems harder than it should be – it is!</p>
<p>My Solution to the trigram:</p>
<blockquote>
<div><ul class="simple">
<li>(<tt class="docutils literal"><span class="pre">dict.setdefault()</span></tt> trick...)</li>
</ul>
</div></blockquote>
<p><tt class="docutils literal"><span class="pre">global</span></tt> keyword?</p>
</div>
<div class="section" id="unicode-notes">
<h3>Unicode Notes<a class="headerlink" href="#unicode-notes" title="Permalink to this headline">¶</a></h3>
<p>To put unicode in your source file, put:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span>
<span class="c"># -*- coding: utf-8 -*-</span>
</pre></div>
</div>
<p>at the top of your file ... and be sure to save it as utf-8!
(file->save with encoding in Sublime)</p>
<p>You also might want to put:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>
</pre></div>
</div>
<p>Additional notes on using Unicode in Python see:</p>
<blockquote>
<div><a class="reference internal" href="supplements/unicode.html#unicode-supplement"><em>Unicode in Python 2</em></a></div></blockquote>
</div>
</div>
<div class="section" id="object-oriented-programming">
<h2>Object Oriented Programming<a class="headerlink" href="#object-oriented-programming" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id1">
<h3>Object Oriented Programming<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p>More about Python implementation than OO design/strengths/weaknesses</p>
<p>One reason for this:</p>
<p>Folks can’t even agree on what OO “really” means</p>
<p>See: The Quarks of Object-Oriented Development</p>
<blockquote>
<div><ul class="simple">
<li>Deborah J. Armstrong</li>
</ul>
</div></blockquote>
<p><a class="reference external" href="http://agp.hx0.ru/oop/quarks.pdf">http://agp.hx0.ru/oop/quarks.pdf</a></p>
<p>Is Python a “True” Object-Oriented Language?</p>
<p>(Doesn’t support full encapsulation, doesn’t <em>require</em>
classes, etc...)</p>
<p class="center large">I don’t Care!</p>
<p>Good software design is about code re-use, clean separation of concerns,
refactorability, testability, etc...</p>
<dl class="docutils">
<dt>OO can help with all that, but:</dt>
<dd><ul class="first last simple">
<li>It doesn’t guarantee it</li>
<li>It can get in the way</li>
</ul>
</dd>
</dl>
<p>Python is a Dynamic Language</p>
<p>That clashes with “pure” OO</p>
<dl class="docutils">
<dt>Think in terms of what makes sense for your project</dt>
<dd>– not any one paradigm of software design.</dd>
</dl>
<p>So what is “object oriented programming”?</p>
<blockquote>
<div>“Objects can be thought of as wrapping their data
within a set of functions designed to ensure that
the data are used appropriately, and to assist in
that use”</div></blockquote>
<p><a class="reference external" href="http://en.wikipedia.org/wiki/Object-oriented_programming">http://en.wikipedia.org/wiki/Object-oriented_programming</a></p>
<p>Even simpler:</p>
<p>“Objects are data and the functions that act on them in one place.”</p>
<p>This is the core of “encapsulation”</p>
<p>In Python: just another namespace.</p>
<p>The OO buzzwords:</p>
<blockquote>
<div><ul class="simple">
<li>data abstraction</li>
<li>encapsulation</li>
<li>modularity</li>
<li>polymorphism</li>
<li>inheritance</li>
</ul>
</div></blockquote>
<p>Python does all of this, though it doesn’t enforce it.</p>
<p>You can do OO in C</p>
<p>(see the GTK+ project)</p>
<p>“OO languages” give you some handy tools to make it easier (and safer):</p>
<blockquote>
<div><ul class="simple">
<li>polymorphism (duck typing gives you this anyway)</li>
<li>inheritance</li>
</ul>
</div></blockquote>
<p>OO is the dominant model for the past couple decades</p>
<p>You will need to use it:</p>
<ul class="simple">
<li>It’s a good idea for a lot of problems</li>
<li>You’ll need to work with OO packages</li>
</ul>
<p>(Even a fair bit of the standard library is Object Oriented)</p>
<dl class="docutils">
<dt>class</dt>
<dd>A category of objects: particular data and behavior: A “circle” (same as a type in python)</dd>
<dt>instance</dt>
<dd>A particular object of a class: a specific circle</dd>
<dt>object</dt>
<dd>The general case of a instance – really any value (in Python anyway)</dd>
<dt>attribute</dt>
<dd>Something that belongs to an object (or class): generally thought of
as a variable, or single object, as opposed to a ...</dd>
<dt>method</dt>
<dd>A function that belongs to a class</dd>
</dl>
<p class="center">Note that in python, functions are first class objects, so a method <em>is</em> an attribute</p>
</div>
</div>
<div class="section" id="python-classes">
<h2>Python Classes<a class="headerlink" href="#python-classes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id2">
<h3>Python Classes<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">class</span></tt> statement</p>
<p><tt class="docutils literal"><span class="pre">class</span></tt> creates a new type object:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [4]: </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</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>
<p>A class is a type – interesting!</p>
<p>It is created when the statement is run – much like <tt class="docutils literal"><span class="pre">def</span></tt></p>
<p>You don’t <em>have</em> to subclass from <tt class="docutils literal"><span class="pre">object</span></tt>, but you <em>should</em></p>
<p>(note on “new style” classes)</p>
<p>About the simplest class you can write</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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="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>Basic Structure of a real class:</p>
<div class="highlight-python"><div class="highlight"><pre><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="c"># everything defined in here is in the class namespace</span>
<span class="k">def</span> <span class="nf">__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>
<span class="c">## create an instance of the class</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>
<span class="c">## access the attributes</span>
<span class="k">print</span> <span class="s">"p.x is:"</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">x</span>
<span class="k">print</span> <span class="s">"p.y is:"</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span>
</pre></div>
</div>
<p>see: <tt class="docutils literal"><span class="pre">Examples/Session06/simple_class</span></tt></p>
<p>The Initializer</p>
<p>The <tt class="docutils literal"><span class="pre">__init__</span></tt> special method is 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"><div class="highlight"><pre><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="nf">__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"><div class="highlight"><pre><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>What is this <tt class="docutils literal"><span class="pre">self</span></tt> thing?</p>
<p>The instance of the class is passed as the first parameter for every method.</p>
<p>“<tt class="docutils literal"><span class="pre">self</span></tt>” is only a convention – but you DO want to use it.</p>
<div class="highlight-python"><div class="highlight"><pre><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="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 <tt class="docutils literal"><span class="pre">self.</span></tt> attribute is kept in the instance
name space – <tt class="docutils literal"><span class="pre">self</span></tt> <em>is</em> the instance.</p>
<p>That’s where all the instance-specific data is.</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"red"</span>
<span class="k">def</span> <span class="nf">__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 <tt class="docutils literal"><span class="pre">def</span></tt> are class attributes as well.</p>
<p>The class is one namespace, the instance is another.</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"red"</span>
<span class="o">...</span>
<span class="k">def</span> <span class="nf">get_color</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="s">'red'</span>
</pre></div>
</div>
<p>class attributes are accessed with <tt class="docutils literal"><span class="pre">self</span></tt> also.</p>
<p>Typical methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="s">"red"</span>
<span class="k">def</span> <span class="nf">__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">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>
</pre></div>
</div>
<p>Methods take some parameters, manipulate the attributes in <tt class="docutils literal"><span class="pre">self</span></tt>.</p>
<p>They may or may not return something useful.</p>
<p>Gotcha!</p>
<div class="highlight-python"><div class="highlight"><pre><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 2</p>
<p><tt class="docutils literal"><span class="pre">self</span></tt> is implicitly passed in for you by python.</p>
<p>(demo of bound vs. unbound methods)</p>
</div>
<div class="section" id="lab-homework">
<h3>LAB / homework<a class="headerlink" href="#lab-homework" title="Permalink to this headline">¶</a></h3>
<p>Let’s say you need to render some html..</p>
<p>The goal is to build a set of classes that render an html page.</p>
<p><tt class="docutils literal"><span class="pre">Examples/Session06/sample_html.html</span></tt></p>
<p>We’ll start with a single class, then add some sub-classes to specialize the behavior</p>
<p>Details in:</p>
<p><a class="reference internal" href="homework/html_builder.html#homework-html-renderer"><em>HTML Renderer Homework Assignment</em></a></p>
<p>Let’s see if we can do step 1. in class...</p>
</div>
</div>
<div class="section" id="subclassing-inheritance">
<h2>Subclassing/Inheritance<a class="headerlink" href="#subclassing-inheritance" title="Permalink to this headline">¶</a></h2>
<div class="section" id="inheritance">
<h3>Inheritance<a class="headerlink" href="#inheritance" title="Permalink to this headline">¶</a></h3>
<p>In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object.</p>
<p>Objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or super classes.</p>
<p>The resulting classes are known as derived classes or subclasses.</p>
<p>(<a class="reference external" href="http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29">http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29</a>)</p>
</div>
<div class="section" id="subclassing">
<h3>Subclassing<a class="headerlink" href="#subclassing" title="Permalink to this headline">¶</a></h3>
<p>A subclass “inherits” all the attributes (methods, etc) of the parent class.</p>
<p>You can then change (“override”) some or all of the attributes to change the behavior.</p>
<p>You can also add new attributes to extend the behavior.</p>
<p>The simplest subclass in Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">A_subclass</span><span class="p">(</span><span class="n">The_superclass</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">A_subclass</span></tt> now has exactly the same behavior as <tt class="docutils literal"><span class="pre">The_superclass</span></tt></p>
<p>NOTE: when we put <tt class="docutils literal"><span class="pre">object</span></tt> in there, it means we are deriving from object – getting core functionality of all objects.</p>
</div>
<div class="section" id="overriding-attributes">
<h3>Overriding attributes<a class="headerlink" href="#overriding-attributes" title="Permalink to this headline">¶</a></h3>
<p>Overriding is as simple as creating a new attribute with the same name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="s">"red"</span>
<span class="o">...</span>
<span class="k">class</span> <span class="nc">NewCircle</span><span class="p">(</span><span class="n">Circle</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="s">"blue"</span>
<span class="o">>>></span> <span class="n">nc</span> <span class="o">=</span> <span class="n">NewCircle</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">nc</span><span class="o">.</span><span class="n">color</span>
<span class="n">blue</span>
</pre></div>
</div>
<p>all the <tt class="docutils literal"><span class="pre">self</span></tt> instances will have the new attribute.</p>
</div>
<div class="section" id="overriding-methods">
<h3>Overriding methods<a class="headerlink" href="#overriding-methods" title="Permalink to this headline">¶</a></h3>
<p>Same thing, but with methods (remember, a method <em>is</em> an attribute in python)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</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="sd">"""grows the circle's diameter by factor"""</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="k">class</span> <span class="nc">NewCircle</span><span class="p">(</span><span class="n">Circle</span><span class="p">):</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="sd">"""grows the area by factor..."""</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">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>all the instances will have the new method</p>
<dl class="docutils">
<dt>Here’s a program design suggestion:</dt>
<dd><p class="first">whenever you override a method, the
interface of the new method should be the same as the old. It should take
the same parameters, return the same type, and obey the same preconditions
and postconditions.</p>
<p class="last">If you obey this rule, you will find that any function
designed to work with an instance of a superclass, like a Deck, will also work
with instances of subclasses like a Hand or PokerHand. If you violate this
rule, your code will collapse like (sorry) a house of cards.</p>
</dd>
</dl>
<p>[ThinkPython 18.10]</p>
<p>( Demo of class vs. instance attributes )</p>
</div>
</div>
<div class="section" id="more-on-subclassing">
<h2>More on Subclassing<a class="headerlink" href="#more-on-subclassing" title="Permalink to this headline">¶</a></h2>
<div class="section" id="overriding-init">
<h3>Overriding __init__<a class="headerlink" href="#overriding-init" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">__init__</span></tt> common method to override}</p>
<p>You often need to call the super class <tt class="docutils literal"><span class="pre">__init__</span></tt> as well</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="s">"red"</span>
<span class="k">def</span> <span class="nf">__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="o">...</span>
<span class="k">class</span> <span class="nc">CircleR</span><span class="p">(</span><span class="n">Circle</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">radius</span><span class="p">):</span>
<span class="n">diameter</span> <span class="o">=</span> <span class="n">radius</span><span class="o">*</span><span class="mi">2</span>
<span class="n">Circle</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">diameter</span><span class="p">)</span>
</pre></div>
</div>
<p>exception to: “don’t change the method signature” rule.</p>
</div>
<div class="section" id="more-subclassing">
<h3>More subclassing<a class="headerlink" href="#more-subclassing" title="Permalink to this headline">¶</a></h3>
<p>You can also call the superclass’ other methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">def</span> <span class="nf">get_area</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="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="p">(</span><span class="n">diameter</span><span class="o">/</span><span class="mf">2.0</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="k">class</span> <span class="nc">CircleR2</span><span class="p">(</span><span class="n">Circle</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">def</span> <span class="nf">get_area</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">Circle</span><span class="o">.</span><span class="n">get_area</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">radius</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>There is nothing special about <tt class="docutils literal"><span class="pre">__init__</span></tt> except that it gets called
automatically when you instantiate an instance.</p>
</div>
<div class="section" id="when-to-subclass">
<h3>When to Subclass<a class="headerlink" href="#when-to-subclass" title="Permalink to this headline">¶</a></h3>
<p>“Is a” relationship: Subclass/inheritance</p>
<p>“Has a” relationship: Composition</p>
<p>“Is a” vs “Has a”</p>
<p>You may have a class that needs to accumulate an arbitrary number of objects.</p>
<p>A list can do that – so should you subclass list?</p>
<p>Ask yourself:</p>
<p>– <strong>Is</strong> your class a list (with some extra functionality)?</p>
<p>or</p>
<p>– Does you class <strong>have</strong> a list?</p>
<p>You only want to subclass list if your class could be used anywhere a list can be used.</p>
</div>
<div class="section" id="attribute-resolution-order">
<h3>Attribute resolution order<a class="headerlink" href="#attribute-resolution-order" title="Permalink to this headline">¶</a></h3>
<p>When you access an attribute:</p>
<p><tt class="docutils literal"><span class="pre">An_Instance.something</span></tt></p>
<p>Python looks for it in this order:</p>
<blockquote>
<div><ul class="simple">
<li>Is it an instance attribute ?</li>
<li>Is it a class attribute ?</li>
<li>Is it a superclass attribute ?</li>
<li>Is it a super-superclass attribute ?</li>
<li>...</li>
</ul>
</div></blockquote>
<p>It can get more complicated...</p>
<p><a class="reference external" href="http://www.python.org/getit/releases/2.3/mro/">http://www.python.org/getit/releases/2.3/mro/</a></p>
<p><a class="reference external" href="http://python-history.blogspot.com/2010/06/method-resolution-order.html">http://python-history.blogspot.com/2010/06/method-resolution-order.html</a></p>
</div>
<div class="section" id="what-are-python-classes-really">
<h3>What are Python classes, really?<a class="headerlink" href="#what-are-python-classes-really" title="Permalink to this headline">¶</a></h3>
<p>Putting aside the OO theory...</p>
<p>Python classes are:</p>
<blockquote>
<div><ul class="simple">
<li>Namespaces<ul>
<li>One for the class object</li>
<li>One for each instance</li>
</ul>
</li>
<li>Attribute resolution order</li>
<li>Auto tacking-on of <tt class="docutils literal"><span class="pre">self</span></tt> when methods are called</li>
</ul>
</div></blockquote>
<p>That’s about it – really!</p>
</div>
<div class="section" id="type-based-dispatch">
<h3>Type-Based dispatch<a class="headerlink" href="#type-based-dispatch" title="Permalink to this headline">¶</a></h3>
<p>You’ll see code that looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">A_Class</span><span class="p">):</span>
<span class="n">Do_something_with_other</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">Do_something_else</span>
</pre></div>
</div>
<p>Usually better to use “duck typing” (polymorphism)</p>
<p>But when it’s called for:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">isinstance()</span></tt></li>
<li><tt class="docutils literal"><span class="pre">issubclass()</span></tt></li>
</ul>
</div></blockquote>
<p>GvR: “Five Minute Multi- methods in Python”:</p>
<p><a class="reference external" href="http://www.artima.com/weblogs/viewpost.jsp?thread=101605">http://www.artima.com/weblogs/viewpost.jsp?thread=101605</a></p>
<p><a class="reference external" href="http://www.python.org/getit/releases/2.3/mro/">http://www.python.org/getit/releases/2.3/mro/</a></p>
<p><a class="reference external" href="http://python-history.blogspot.com/2010/06/method-resolution-order.html">http://python-history.blogspot.com/2010/06/method-resolution-order.html</a></p>
</div>
<div class="section" id="wrap-up">
<h3>Wrap Up<a class="headerlink" href="#wrap-up" title="Permalink to this headline">¶</a></h3>
<p>Thinking OO in Python:</p>
<p>Think about what makes sense for your code:</p>
<ul class="simple">
<li>Code re-use</li>
<li>Clean APIs</li>
<li>...</li>
</ul>
<p>Don’t be a slave to what OO is <em>supposed</em> to look like.</p>
<p>Let OO work for you, not <em>create</em> work for you</p>
<p>OO in Python:</p>
<p>The Art of Subclassing: <em>Raymond Hettinger</em></p>
<p><a class="reference external" href="http://pyvideo.org/video/879/the-art-of-subclassing">http://pyvideo.org/video/879/the-art-of-subclassing</a></p>
<p>“classes are for code re-use – not creating taxonomies”</p>
<p>Stop Writing Classes: <em>Jack Diederich</em></p>
<p><a class="reference external" href="http://pyvideo.org/video/880/stop-writing-classes">http://pyvideo.org/video/880/stop-writing-classes</a></p>
<p>“If your class has only two methods – and one of them is <tt class="docutils literal"><span class="pre">__init__</span></tt>
– you don’t need a class”</p>
</div>
<div class="section" id="homework">
<h3>Homework<a class="headerlink" href="#homework" title="Permalink to this headline">¶</a></h3>
<p>Build an html rendering system:</p>
<p><a class="reference internal" href="homework/html_builder.html#homework-html-renderer"><em>HTML Renderer Homework Assignment</em></a></p>
<div class="line-block">
<div class="line"><br /></div>
</div>
<p>You will build an html generator, using:</p>
<ul class="simple">
<li>A Base Class with a couple methods</li>
<li>Subclasses overriding class attributes</li>
<li>Subclasses overriding a method</li>
<li>Subclasses overriding the <tt class="docutils literal"><span class="pre">__init__</span></tt></li>
</ul>
<p>These are the core OO approaches</p>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="session07.html" class="btn btn-neutral float-right" title="Session Seven: Testing, More OO"/>Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="session05.html" class="btn btn-neutral" title="Session Five: Advanced Argument passing, List and Dict Comprehensions, Lambda and Functional programming"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2014, Christopher Barker, Cris Ewing, .
</p>
</div>
<a href="https://github.com/snide/sphinx_rtd_theme">Sphinx theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'1.3',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>