|
| 1 | +Creating Classes |
| 2 | +----------------------------------------------------------------- |
| 3 | + |
| 4 | +Look the code below. It defines a class. it also declares *methods* which are |
| 5 | +functions that are defined inside of a class. |
| 6 | +One of the methods, ``__init__``, is automatically called when a new object is |
| 7 | +created by the class. One of the methods, ``__str__``, is automatically |
| 8 | +called when you print an object of the class. These methods start and end with two underscores. |
| 9 | + |
| 10 | +A Book Class |
| 11 | +====================================================== |
| 12 | + |
| 13 | +.. activecode:: class_book_ac1_v2 |
| 14 | + :caption: A class to represent a book |
| 15 | + |
| 16 | + Run the following code |
| 17 | + ~~~~ |
| 18 | + class Book: |
| 19 | + """ Represents a book object """ |
| 20 | + |
| 21 | + # initializes the values in a new object called self |
| 22 | + def __init__(self, title, author): |
| 23 | + self.title = title # set title in self to the passed title |
| 24 | + self.author = author # set author in self to the passsed author |
| 25 | + |
| 26 | + # returns a string with information about the object self |
| 27 | + def __str__(self): |
| 28 | + return "title: " + self.title + " author: " + self.author |
| 29 | + |
| 30 | + def main(): |
| 31 | + # calls the __init__ method |
| 32 | + b2 = Book("A Wrinkle in Time", "M. L'Engle") |
| 33 | + |
| 34 | + # calls the __str__ method |
| 35 | + print(b2) |
| 36 | + |
| 37 | + # calls the __init__ method |
| 38 | + b1 = Book("Goodnight Moon", "Margaret Wise Brown") |
| 39 | + |
| 40 | + # calls the __str__ method |
| 41 | + print(b1) |
| 42 | + |
| 43 | + main() |
| 44 | + |
| 45 | + |
| 46 | +Creating More Objects |
| 47 | +====================================================== |
| 48 | + |
| 49 | +Once you have defined a class you can use it to create many objects. |
| 50 | + |
| 51 | +.. activecode:: class_person_ac2 |
| 52 | + :caption: A class to represent a Person |
| 53 | + |
| 54 | + Change the following main function to add a new person object. |
| 55 | + ~~~~ |
| 56 | + class Person: |
| 57 | + """ Represents a person object """ |
| 58 | + |
| 59 | + # initializes the values in a new object called self |
| 60 | + def __init__(self, first, last): |
| 61 | + self.first = first # set first in self to the passed first |
| 62 | + self.last = last # set last in self to the passed last |
| 63 | + |
| 64 | + # returns a string with information about the object self |
| 65 | + def __str__(self): |
| 66 | + return self.first + " " + self.last |
| 67 | + |
| 68 | + def main(): |
| 69 | + # calls the __init__ method |
| 70 | + p1 = Person("Barbara", "Ericson") |
| 71 | + |
| 72 | + # calls the __str__ method |
| 73 | + print(p1) |
| 74 | + |
| 75 | + # create an object for another person (calls the __init__ method) |
| 76 | + |
| 77 | + # print the new object (calls the __str__ method) |
| 78 | + |
| 79 | + main() |
| 80 | + |
| 81 | +Add a Method to a Class |
| 82 | +====================================================== |
| 83 | + |
| 84 | +You can add a new method to a class by adding a new function inside the class. For example, you can add the ``initials`` |
| 85 | +method to the Person class. The name of the function |
| 86 | +doesn't need to have any underscores in it. It only needs to start and end with double |
| 87 | +underscores if it is a special method like ``__init__`` or ``__str__``. It does need to take |
| 88 | +the current object which is by convention referred to as ``self``. |
| 89 | + |
| 90 | +.. activecode:: class_person_init_ac1_v2 |
| 91 | + :caption: A class to represent a Person |
| 92 | + |
| 93 | + The following Person class has an ``initials`` method that returns |
| 94 | + a string with the first letter in the first name and the first letter in |
| 95 | + the last name in lowercase. |
| 96 | + ~~~~ |
| 97 | + class Person: |
| 98 | + """ Represents a person object """ |
| 99 | + |
| 100 | + # initializes the values in a new object called self |
| 101 | + def __init__(self, first, last): |
| 102 | + self.first = first # set first in self to the passed first |
| 103 | + self.last = last # set last in self to the passed last |
| 104 | + |
| 105 | + # returns a string with information about the object self |
| 106 | + def __str__(self): |
| 107 | + return self.first + " " + self.last |
| 108 | + |
| 109 | + # returns the first characters of the first and last name in lowercase |
| 110 | + def initials(self): |
| 111 | + return self.first[0].lower() + self.last[0].lower() |
| 112 | + |
| 113 | + def main(): |
| 114 | + # calls the __init__ method |
| 115 | + p1 = Person("Barbara", "Ericson") |
| 116 | + |
| 117 | + # calls the __str__ method |
| 118 | + print(p1) |
| 119 | + |
| 120 | + # calls the initials method |
| 121 | + print(p1.initials()) |
| 122 | + |
| 123 | + main() |
| 124 | + |
| 125 | + ==== |
| 126 | + from unittest.gui import TestCaseGui |
| 127 | + class myTests(TestCaseGui): |
| 128 | + |
| 129 | + def testOne(self): |
| 130 | + p1 = Person("Barbara", "Ericson") |
| 131 | + self.assertEqual(p1.initials(),'be',"testing initials for Barbara Ericson") |
| 132 | + p2 = Person("Enoch", "Obe") |
| 133 | + self.assertEqual(p2.initials(),"eo", "testing initials for Enoch Obe") |
| 134 | + |
| 135 | + myTests().main() |
| 136 | + |
| 137 | +Feedback |
| 138 | +================================== |
| 139 | + |
| 140 | +.. shortanswer:: class-intro-classes-ps-sa |
| 141 | + |
| 142 | + Please provide feedback here. Please share any comments, problems, or suggestions. |
| 143 | + |
| 144 | +What to do next |
| 145 | +============================ |
| 146 | + |
| 147 | +.. raw:: html |
| 148 | + |
| 149 | + <p>Click on the following link to take the pre survey : <b><a id="class-survey"> <font size="+2">Pre Survey</font></a></b></p> |
| 150 | + |
| 151 | +.. raw:: html |
| 152 | + |
| 153 | + <script type="text/javascript" > |
| 154 | +
|
| 155 | + window.onload = function() { |
| 156 | +
|
| 157 | + a = document.getElementById("class-survey") |
| 158 | + a.href = "class-presurvey.html" |
| 159 | + }; |
| 160 | +
|
| 161 | + </script> |
| 162 | + |
| 163 | + |
0 commit comments