Expanding the User Class with Instance Methods
Understand what instance methods are.
We'll cover the following...
Until now, we’ve had users post chirps manually by creating Post
objects and passing the user as an argument. But as our project grows, we begin to realize that it would be far more efficient and intuitive if users could create posts directly without having to construct Post
objects manually every time.
Why is this a problem?
In our current design, users must know how to create a Post
object, which isn’t very user-friendly or scalable. As we add more features (like liking posts or engaging with comments), this manual process becomes even more cumbersome. Wouldn’t it be much better if a user could simply call a method like create_post()
and have everything set up automatically?
This is where the relationship between a class and its objects becomes essential. A class is like a blueprint—it defines the structure and behavior (methods) that all its objects (instances) will have. When you create an object from a class, it inherits all the defined behaviors and properties. In object-oriented programming, methods are functions defined within a class that act on the object’s data using the self
parameter. By tying functionality like create_post()
to the User
class as an instance method, we enable each user object to “do something” on its own—such as creating posts or liking posts—without ...