Skip to content

Commit 1794a24

Browse files
feat: add classes and objects lectures (freeCodeCamp#61327)
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
1 parent 19caaaf commit 1794a24

4 files changed

Lines changed: 633 additions & 48 deletions

File tree

curriculum/challenges/_meta/lecture-classes-and-objects/meta.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
"blockType": "lecture",
66
"blockLayout": "challenge-list",
77
"superBlock": "full-stack-developer",
8-
"challengeOrder": [{ "id": "68420bd261d0d35f61922d4b", "title": "Step 1" }],
8+
"challengeOrder": [
9+
{
10+
"id": "68420bd261d0d35f61922d4b",
11+
"title": "How Do Classes Work and How Do They Differ From Objects?"
12+
},
13+
{
14+
"id": "6874b5fc38f90f25e18093ce",
15+
"title": "What Are Methods and Attributes, and How Do They Work?"
16+
},
17+
{
18+
"id": "6874b7d3b286c538b39d0c25",
19+
"title": "What Are Special Methods and What Are They Used For?"
20+
}
21+
],
922
"helpCategory": "Python"
10-
}
23+
}
Lines changed: 129 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,220 @@
11
---
22
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?
64
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
106
---
117

128
# --description--
139

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+
1598

1699
# --questions--
17100

18101
## --text--
19102

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+
```
21116

22117
## --answers--
23118

24-
Answer 1.1
119+
`Rex says Woof!`
25120

26121
### --feedback--
27122

28-
Feedback 1
123+
Look at what is being printed: is it calling a method or accessing an attribute?
29124

30125
---
31126

32-
Answer 1.2
127+
`name`
33128

34129
### --feedback--
35130

36-
Feedback 1
131+
Look at what is being printed: is it calling a method or accessing an attribute?
37132

38133
---
39134

40-
Answer 1.3
41-
42-
### --feedback--
43-
44-
Feedback 1
135+
`Rex`
45136

46137
---
47138

48-
Answer 1.4
139+
Error
49140

50141
### --feedback--
51142

52-
Feedback 1
143+
Look at what is being printed: is it calling a method or accessing an attribute?
53144

54145
## --video-solution--
55146

56-
5
147+
3
57148

58149
## --text--
59150

60-
Question 2
151+
What is the special method that gets automatically called when a new object is created?
61152

62153
## --answers--
63154

64-
Answer 2.1
155+
`__create_object__`
65156

66157
### --feedback--
67158

68-
Feedback 2
159+
Think about what initializes the attributes an object will have.
69160

70161
---
71162

72-
Answer 2.2
73-
74-
### --feedback--
75-
76-
Feedback 2
163+
`__init__`
77164

78165
---
79166

80-
Answer 2.3
167+
`__new__`
81168

82169
### --feedback--
83170

84-
Feedback 2
171+
Think about what initializes the attributes an object will have.
85172

86173
---
87174

88-
Answer 2.4
175+
`__setup__`
89176

90177
### --feedback--
91178

92-
Feedback 2
179+
Think about what initializes the attributes an object will have.
93180

94181
## --video-solution--
95182

96-
5
183+
2
97184

98185
## --text--
99186

100-
Question 3
187+
What is the blueprint or template for creating objects?
101188

102189
## --answers--
103190

104-
Answer 3.1
191+
A variable
105192

106193
### --feedback--
107194

108-
Feedback 3
195+
Think about what defines the structure and behavior of objects created from it.
109196

110197
---
111198

112-
Answer 3.2
199+
A function
113200

114201
### --feedback--
115202

116-
Feedback 3
203+
Think about what defines the structure and behavior of objects created from it.
117204

118205
---
119206

120-
Answer 3.3
121-
122-
### --feedback--
123-
124-
Feedback 3
207+
A class
125208

126209
---
127210

128-
Answer 3.4
211+
A loop
129212

130213
### --feedback--
131214

132-
Feedback 3
215+
Think about what defines the structure and behavior of objects created from it.
133216

134217
## --video-solution--
135218

136-
5
219+
3
137220

0 commit comments

Comments
 (0)