Adding comments
- Python course
- Intermediate
- Lesson 61/116
- 6 min
Write the comment flow with add_comment.
Link comments to a Post
In the first lesson we defined the Comment class but didn't link it to posts. Now we add a field comments: list = field(default_factory=list) to the Post class. Every post then has its own comment list.
Next we write the add_comment method inside Post: self.comments.append(comment). To add a comment you call p.add_comment(Comment(author="Ali", text="Nice")). len(p.comments) gives the comment count.
Where the method lives matters
You write add_comment inside the Post class because a comment belongs to a post. Putting it on Blog would be wrong; a blog holds many posts and wouldn't know which post to add the comment to.
The method body calls self.comments.append(comment). Here self is the current post instance and comments is its own list. This separation keeps each post's comments apart.
Practice Add comments to the Post class
Add a field comments: list = field(default_factory=list) to the Post class above and write def add_comment(self, comment): with the body self.comments.append(comment). Then create p = Post(title="First", author="Ayşe"), add two comments and print the count with print(len(p.comments)).
Starter code
from dataclasses import dataclass, field
@dataclass
class Post:
title: str = ""
content: str = ""
author: str = ""
@dataclass
class Comment:
author: str = ""
text: str = ""
@dataclass
class Blog:
posts: list = field(default_factory=list)
def add_post(self, post):
self.posts.append(post)
# Add a comments field and an add_comment method to Post
# Create a post, add two comments, print the countSample solution
from dataclasses import dataclass, field
@dataclass
class Post:
title: str = ""
content: str = ""
author: str = ""
comments: list = field(default_factory=list)
def add_comment(self, comment):
self.comments.append(comment)
@dataclass
class Comment:
author: str = ""
text: str = ""
@dataclass
class Blog:
posts: list = field(default_factory=list)
def add_post(self, post):
self.posts.append(post)
p = Post(title="First", author="Ayşe")
p.add_comment(Comment(author="Ali", text="Nice one"))
p.add_comment(Comment(author="Veli", text="Thanks"))
print(len(p.comments))Expected output
2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the comment-adding concepts
| Term | Meaning |
|---|---|
comments |
A post's comment list |
add_comment |
Comment-adding method |
append |
Add to the end |
len |
Count function |
2 Which class should add_comment belong to?
- The Blog class
- The Comment class
- The Post class Correct answer
- Left at module level
Explanation: A comment is tied to a post, so add_comment lives on the Post class.
3 Parameter of add_comment
def add_comment(self, ____):
self.comments.append(comment)
def add_comment(self, comment):
self.comments.append(comment)
Explanation: A single comment object is taken as the parameter.