Adding a Post Class
Learn how to add the Post class to your Chirpy app.
We'll cover the following...
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:
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 postself.author = author # Store the user who created itself.likes = [ ] # List of users who liked the post
What’s happening here?
self.content
: Stores the text of the post.self.author
: Stores theUser
object that made the post.self.likes
: A list that will later hold users who like this post.
Now, let’s ...