|
1 | 1 | --- |
2 | 2 | id: 68420bd261d0d35f61922d4b |
3 | | -# title needs to be updated to correct title when lectures are finalized |
4 | | -title: Classes and Objects |
5 | | -# change back to 11 when video is added |
| 3 | +title: How Do Classes Work and How Do They Differ From Objects? |
6 | 4 | challengeType: 19 |
7 | | -# videoId: new-id-goes-here-when-ready |
8 | | -# dashedName needs to be updated to correct title when lectures are finalized |
9 | | -dashedName: lecture-classes-and-objects |
| 5 | +dashedName: how-do-classes-work-and-how-do-they-differ-from-objects |
10 | 6 | --- |
11 | 7 |
|
12 | 8 | # --description-- |
13 | 9 |
|
14 | | -Watch the video or read the transcript and answer the questions below. |
| 10 | +In Python, classes and objects work hand in hand to organize and manage data. You build a class to define shared behavior, then create objects that use those behaviors. |
| 11 | + |
| 12 | +In other words, a class is like a blueprint or template you use to create objects with. |
| 13 | + |
| 14 | +Let's look at what classes are, and how to use them to create objects. |
| 15 | + |
| 16 | +To create a class, you use the `class` keyword followed by the name of the class and a colon. Then within the class, you can add an initializer, along with any attributes and methods. |
| 17 | + |
| 18 | +Attributes are like variables within a class, and are used to store data. Methods are functions defined within a class, and are the actions objects created with a class can perform. |
| 19 | + |
| 20 | +Here's the basic syntax of a class: |
| 21 | + |
| 22 | +```python |
| 23 | +class ClassName: |
| 24 | + def __init__(self, name, age): |
| 25 | + self.name = name |
| 26 | + self.age = age |
| 27 | + |
| 28 | + def sample_method(self): |
| 29 | + print(self.name.upper()) |
| 30 | +``` |
| 31 | + |
| 32 | +* `class ClassName` is made up of the `class` keyword to create a class, followed by the name of the class, here called `ClassName`. It is common in Python to use the **PascalCase** convention when naming classes. |
| 33 | + |
| 34 | +* `def __init__(self, name, age)` is the special method automatically called when a new object is created. It initializes the attributes of the objects that will be created with the class. |
| 35 | + |
| 36 | + In addition to that, the first parameter of `__init__` is always a reference to the specific object being created or used. By convention, this parameter is named `self`, but technically, you can use any name. `self` lets you access the object's own attributes and methods. |
| 37 | + |
| 38 | +* `self.name = name` and `self.age = age` are the attributes the objects will have. |
| 39 | + |
| 40 | +* `def sample_method(self):` is the method each object created can call. |
| 41 | + |
| 42 | +* `print(self.name.upper())` is what the `sample_method` method will do, in this case, it prints the name in uppercase. |
| 43 | + |
| 44 | + |
| 45 | +If that all sounds like a lot, don't worry. Let's take a look at a similar example of a `Dog` class, and how you can create objects from that: |
| 46 | + |
| 47 | +```python |
| 48 | +class Dog: |
| 49 | + def __init__(self, name, age): |
| 50 | + self.name = name |
| 51 | + self.age = age |
| 52 | + |
| 53 | + def bark(self): |
| 54 | + print(f"{self.name.upper()} says woof woof!") |
| 55 | +``` |
| 56 | + |
| 57 | +With this `Dog` class, you can create an object. Here's the basic syntax for creating objects from a class: |
| 58 | + |
| 59 | +```python |
| 60 | +object_1 = ClassName(attribute_1, attribute_2) |
| 61 | +object_2 = ClassName(attribute_1, attribute_2) |
| 62 | +``` |
| 63 | + |
| 64 | +You can also call any of the methods defined in the class from each object: |
| 65 | + |
| 66 | +```python |
| 67 | +object_1.method_name() |
| 68 | +object_2.method_name() |
| 69 | +``` |
| 70 | + |
| 71 | +Now let's create two dogs by using the `Dog` class as the blueprint: |
| 72 | + |
| 73 | +```python |
| 74 | +class Dog: |
| 75 | + def __init__(self, name, age): |
| 76 | + self.name = name |
| 77 | + self.age = age |
| 78 | + |
| 79 | + def bark(self): |
| 80 | + print(f"{self.name.upper()} says woof woof! I'm {self.age} years old!") |
| 81 | + |
| 82 | +dog_1 = Dog("Jack", 3) |
| 83 | +dog_2 = Dog("Thatcher", 5) |
| 84 | + |
| 85 | +# Call the bark method |
| 86 | +dog_1.bark() # JACK says woof woof! I'm 3 years old! |
| 87 | +dog_2.bark() # THATCHER says woof woof! I'm 5 years old! |
| 88 | +``` |
| 89 | + |
| 90 | +As you can see, we create two dog objects using the `Dog` class. When initializing `dog_1`, the string `Jack` and the number `3` are passed, which sets the `name` and `age` attributes for that instance. And `dog_2` is initialized with the string `Thatcher` and number `5` as its `name` and `age`, respectively. |
| 91 | + |
| 92 | +Then when you call the `.bark()` method on `dog_1` and `dog_2`, you can see how both outputs differ, and use the unique `name` and `age` attributes you passed in when creating each object. |
| 93 | + |
| 94 | +In summary, the difference between a class and an object is that a class is the template or the blueprint, and an object is what is created using that template. |
| 95 | + |
| 96 | +Also, a class defines what data and behavior the object should have, and an object holds the actual data and uses that behavior. You write a class once, and you can make many objects from it, each with different data. |
| 97 | + |
15 | 98 |
|
16 | 99 | # --questions-- |
17 | 100 |
|
18 | 101 | ## --text-- |
19 | 102 |
|
20 | | -Question 1 |
| 103 | +What is the output of this code? |
| 104 | + |
| 105 | +```python |
| 106 | +class Dog: |
| 107 | + def __init__(self, name): |
| 108 | + self.name = name |
| 109 | + |
| 110 | + def bark(self): |
| 111 | + print(f"{self.name} says Woof!") |
| 112 | + |
| 113 | +my_dog = Dog("Rex") |
| 114 | +print(my_dog.name) |
| 115 | +``` |
21 | 116 |
|
22 | 117 | ## --answers-- |
23 | 118 |
|
24 | | -Answer 1.1 |
| 119 | +`Rex says Woof!` |
25 | 120 |
|
26 | 121 | ### --feedback-- |
27 | 122 |
|
28 | | -Feedback 1 |
| 123 | +Look at what is being printed: is it calling a method or accessing an attribute? |
29 | 124 |
|
30 | 125 | --- |
31 | 126 |
|
32 | | -Answer 1.2 |
| 127 | +`name` |
33 | 128 |
|
34 | 129 | ### --feedback-- |
35 | 130 |
|
36 | | -Feedback 1 |
| 131 | +Look at what is being printed: is it calling a method or accessing an attribute? |
37 | 132 |
|
38 | 133 | --- |
39 | 134 |
|
40 | | -Answer 1.3 |
41 | | - |
42 | | -### --feedback-- |
43 | | - |
44 | | -Feedback 1 |
| 135 | +`Rex` |
45 | 136 |
|
46 | 137 | --- |
47 | 138 |
|
48 | | -Answer 1.4 |
| 139 | +Error |
49 | 140 |
|
50 | 141 | ### --feedback-- |
51 | 142 |
|
52 | | -Feedback 1 |
| 143 | +Look at what is being printed: is it calling a method or accessing an attribute? |
53 | 144 |
|
54 | 145 | ## --video-solution-- |
55 | 146 |
|
56 | | -5 |
| 147 | +3 |
57 | 148 |
|
58 | 149 | ## --text-- |
59 | 150 |
|
60 | | -Question 2 |
| 151 | +What is the special method that gets automatically called when a new object is created? |
61 | 152 |
|
62 | 153 | ## --answers-- |
63 | 154 |
|
64 | | -Answer 2.1 |
| 155 | +`__create_object__` |
65 | 156 |
|
66 | 157 | ### --feedback-- |
67 | 158 |
|
68 | | -Feedback 2 |
| 159 | +Think about what initializes the attributes an object will have. |
69 | 160 |
|
70 | 161 | --- |
71 | 162 |
|
72 | | -Answer 2.2 |
73 | | - |
74 | | -### --feedback-- |
75 | | - |
76 | | -Feedback 2 |
| 163 | +`__init__` |
77 | 164 |
|
78 | 165 | --- |
79 | 166 |
|
80 | | -Answer 2.3 |
| 167 | +`__new__` |
81 | 168 |
|
82 | 169 | ### --feedback-- |
83 | 170 |
|
84 | | -Feedback 2 |
| 171 | +Think about what initializes the attributes an object will have. |
85 | 172 |
|
86 | 173 | --- |
87 | 174 |
|
88 | | -Answer 2.4 |
| 175 | +`__setup__` |
89 | 176 |
|
90 | 177 | ### --feedback-- |
91 | 178 |
|
92 | | -Feedback 2 |
| 179 | +Think about what initializes the attributes an object will have. |
93 | 180 |
|
94 | 181 | ## --video-solution-- |
95 | 182 |
|
96 | | -5 |
| 183 | +2 |
97 | 184 |
|
98 | 185 | ## --text-- |
99 | 186 |
|
100 | | -Question 3 |
| 187 | +What is the blueprint or template for creating objects? |
101 | 188 |
|
102 | 189 | ## --answers-- |
103 | 190 |
|
104 | | -Answer 3.1 |
| 191 | +A variable |
105 | 192 |
|
106 | 193 | ### --feedback-- |
107 | 194 |
|
108 | | -Feedback 3 |
| 195 | +Think about what defines the structure and behavior of objects created from it. |
109 | 196 |
|
110 | 197 | --- |
111 | 198 |
|
112 | | -Answer 3.2 |
| 199 | +A function |
113 | 200 |
|
114 | 201 | ### --feedback-- |
115 | 202 |
|
116 | | -Feedback 3 |
| 203 | +Think about what defines the structure and behavior of objects created from it. |
117 | 204 |
|
118 | 205 | --- |
119 | 206 |
|
120 | | -Answer 3.3 |
121 | | - |
122 | | -### --feedback-- |
123 | | - |
124 | | -Feedback 3 |
| 207 | +A class |
125 | 208 |
|
126 | 209 | --- |
127 | 210 |
|
128 | | -Answer 3.4 |
| 211 | +A loop |
129 | 212 |
|
130 | 213 | ### --feedback-- |
131 | 214 |
|
132 | | -Feedback 3 |
| 215 | +Think about what defines the structure and behavior of objects created from it. |
133 | 216 |
|
134 | 217 | ## --video-solution-- |
135 | 218 |
|
136 | | -5 |
| 219 | +3 |
137 | 220 |
|
0 commit comments