|
30 | 30 | The best way to understand classes and objects is to see them in action. Let's define a <c>Dog</c> class in Python: |
31 | 31 | </p> |
32 | 32 |
|
33 | | - <program xml:id="python-class-example" language="python"> |
34 | | - <code> |
| 33 | + <listing xml:id="python-class-example"> |
| 34 | + <program language="python"> |
| 35 | + <code> |
35 | 36 | class Dog: |
| 37 | + """ A simple Dog class definition. """ |
36 | 38 | def __init__(self, name, breed, fur_color): |
| 39 | + # constructor method to create a Dog object |
37 | 40 | self.name = name |
38 | 41 | self.breed = breed |
39 | 42 | self.fur_color = fur_color |
40 | | - self.trained = False |
| 43 | + self.trained = False # dogs are not trained by default |
41 | 44 | print("Dog named " + self.name + " created!") |
42 | 45 |
|
43 | 46 | def bark(self): |
| 47 | + # method to make the dog bark |
44 | 48 | print(self.name + " says woof!") |
45 | 49 |
|
46 | 50 | def sit(self): |
47 | | - if self.trained: |
| 51 | + # method to make the dog sit |
| 52 | + if self.trained: # check if the dog has been trained otherwise it will not sit |
48 | 53 | print(self.name + " sits.") |
49 | 54 | else: |
50 | 55 | print(self.name + " has not been trained.") |
51 | 56 |
|
52 | 57 | def train(self): |
| 58 | + # method to train the dog, which will set the trained attribute to True |
53 | 59 | self.trained = True |
54 | | - </code> |
55 | | - </program> |
| 60 | + </code> |
| 61 | + </program> |
| 62 | + </listing> |
56 | 63 |
|
57 | 64 | <p> |
58 | | - Let's unpack what is going on in this code. The first line is where we declare the class definition and name it <c>Dog</c>. Next, we have a special method called <c>__init__</c>. This <c>__init__</c> method is the constructor and is required for every Python class definition. Within the <c>__init__</c> method, attributes are defined. As you can see, the attributes <c>name</c>, <c>breed</c>, and <c>fur_color</c> must be defined when creating a Dog object using this class definition, but the <c>trained</c> attribute is defined within the constructor and is initialized as <c>False</c>. We can also have the <c>__init__</c> method run any code, such as the print statement informing us that a Dog object was created. |
| 65 | + Let's unpack what is going on in <xref ref="python-class-example" text="type-global"/>. The first line is where we declare the class definition and name it <c>Dog</c>. Next, we have a special method called <c>__init__</c>. This <c>__init__</c> method is the constructor and is required for every Python class definition. Within the <c>__init__</c> method, attributes are defined. As you can see, the attributes <c>name</c>, <c>breed</c>, and <c>fur_color</c> must be defined when creating a Dog object using this class definition, but the <c>trained</c> attribute is defined within the constructor and is initialized as <c>False</c>. We can also have the <c>__init__</c> method run any code, such as the print statement informing us that a Dog object was created. |
59 | 66 | </p> |
60 | 67 |
|
61 | 68 | <p> |
|
70 | 77 | <p> |
71 | 78 | Next, we will use this class to create a new <c>Dog</c> object. We will call this new Dog object <c>my_dog</c>: |
72 | 79 | </p> |
73 | | - |
74 | | - <program xml:id="python-class-create-object" interactive="activecode" language="python" > |
| 80 | + <listing xml:id="python-class-create-object" names="python-class-create-object"> |
| 81 | + <program interactive="activecode" language="python" > |
75 | 82 | <code> |
76 | 83 | class Dog: |
| 84 | + """ A simple Dog class definition. """ |
77 | 85 | def __init__(self, name, breed, fur_color): |
| 86 | + # constructor method to create a Dog object |
78 | 87 | self.name = name |
79 | 88 | self.breed = breed |
80 | 89 | self.fur_color = fur_color |
81 | | - self.trained = False |
| 90 | + self.trained = False # dogs are not trained by default |
82 | 91 | print("Dog named " + self.name + " created!") |
83 | 92 |
|
84 | 93 | def bark(self): |
| 94 | + # method to make the dog bark |
85 | 95 | print(self.name + " says woof!") |
86 | 96 |
|
87 | 97 | def sit(self): |
88 | | - if self.trained: |
| 98 | + # method to make the dog sit |
| 99 | + if self.trained: # check if the dog has been trained otherwise it will not sit |
89 | 100 | print(self.name + " sits.") |
90 | 101 | else: |
91 | 102 | print(self.name + " has not been trained.") |
92 | 103 |
|
93 | 104 | def train(self): |
| 105 | + # method to train the dog, which will set the trained attribute to True |
94 | 106 | self.trained = True |
95 | 107 |
|
96 | | - |
| 108 | + # Create a Dog object called my_dog |
97 | 109 | my_dog = Dog("Rex", "pug", "brown") |
98 | 110 | </code> |
99 | 111 | </program> |
100 | | - |
| 112 | + </listing> |
101 | 113 | <p> |
102 | | - In the final line of code, we have created an object called <c>my_dog</c>. We have initialized its attributes, setting <c>name</c> to Rex, <c>breed</c> to pug, and <c>fur_color</c> to brown. |
| 114 | + In the final line of code in <xref ref="python-class-create-object" text="type-global"/>, we have created an object called <c>my_dog</c>. We have initialized its attributes, setting <c>name</c> to Rex, <c>breed</c> to pug, and <c>fur_color</c> to brown. |
103 | 115 | </p> |
104 | 116 |
|
105 | 117 | <p> |
106 | 118 | Now that we have created a <c>Dog</c> object using the class we defined, we can utilize the class's methods: |
107 | 119 | </p> |
108 | 120 |
|
109 | | - <program xml:id="python-class-full" interactive="activecode" language="python"> |
| 121 | + <listing xml:id="python-class-full" names="python-class-full"> |
| 122 | + <program interactive="activecode" language="python"> |
110 | 123 | <code> |
111 | 124 | class Dog: |
| 125 | + """ A simple Dog class definition. """ |
112 | 126 | def __init__(self, name, breed, fur_color): |
| 127 | + # constructor method to create a Dog object |
113 | 128 | self.name = name |
114 | 129 | self.breed = breed |
115 | 130 | self.fur_color = fur_color |
116 | | - self.trained = False |
| 131 | + self.trained = False # dogs are not trained by default |
117 | 132 | print("Dog named " + self.name + " created!") |
118 | 133 |
|
119 | 134 | def bark(self): |
| 135 | + # method to make the dog bark |
120 | 136 | print(self.name + " says woof!") |
121 | 137 |
|
122 | 138 | def sit(self): |
| 139 | + # method to make the dog sit |
123 | 140 | if self.trained: |
124 | 141 | print(self.name + " sits.") |
125 | 142 | else: |
126 | 143 | print(self.name + " has not been trained.") |
127 | 144 |
|
128 | 145 | def train(self): |
| 146 | + # method to train the dog, which will set the trained attribute to True |
129 | 147 | self.trained = True |
130 | 148 |
|
131 | 149 |
|
132 | 150 | my_dog = Dog("Rex", "pug", "brown") |
133 | | - my_dog.bark() |
134 | | - my_dog.sit() |
| 151 | + my_dog.bark() # call the bark method |
| 152 | + my_dog.sit() # call the sit method |
135 | 153 | </code> |
136 | 154 | </program> |
| 155 | + </listing> |
137 | 156 |
|
138 | 157 | <note> |
139 | 158 | <p> |
140 | | - When running the code above, the line <c>Rex has not ben trained.</c> will appear in the output when calling the <c>sit()</c> method. Try adding a one or more lines of code so that <c>Rex sits.</c> appears in the output! |
| 159 | + When running <xref ref="python-class-full" text="type-global"/>, the line <c>Rex has not been trained.</c> will appear in the output when calling the <c>sit()</c> method. Try adding a one or more lines of code so that <c>Rex sits.</c> appears in the output! |
141 | 160 | </p> |
142 | 161 | </note> |
143 | 162 |
|
|
0 commit comments