Adding a Post Class

Learn how to add the Post class to your Chirpy app.

We now have users in Chirpy, our micro-social network.

But what’s the point of a social media platform if users can’t share their thoughts? Right now, our users exist, but they have nothing to say—it’s time to change that!

In this lesson, we’ll introduce a way for users to post short messages or chirps that others can see. To do that, we need a Post class.

What makes a post?

Let’s think about what a chirp (post) should include:

  • Content: The text of the chirp.

  • Author: The user who created it.

  • Likes: A list to keep track of users who liked the post.

Let’s visualize what it looks like:

Press + to interact
We have a Post class in which we have three posts
1 / 4
We have a Post class in which we have three posts

Now that we know what a Post should have, let’s code it!

class Post:
def __init__(self, content, author):
self.content = content # Store the text of the post
self.author = author # Store the user who created it
self.likes = [ ] # List of users who liked the post
The Post class

What’s happening here?

  • self.content: Stores the text of the post.

  • self.author: Stores the User object that made the post.

  • self.likes: A list that will later hold users who like this post.

Now, let’s ...