Search⌘ K
AI Features

Inheritance—Creating a VerifiedUser

Explore inheritance in Python by building a VerifiedUser subclass that inherits from User, adds verification badges, and custom methods. Understand how to extend functionality cleanly without altering the parent class. Practice applying super() to initialize inherited attributes and prepare for advanced user role customizations.

Right now, all users on Chirpy are the same—whether they are regular users or verified ones. But just like in real-world social networks, some users need to stand out. Verified users bring credibility and recognition to the platform, making their presence more distinct.

To reflect this, Chirpy also recognizes verified users, giving them:

  • A verification badge to indicate their authenticity.

  • Special privileges—perhaps their posts appear more prominently.

Now, we could solve this problem by adding an extra attribute like is_verified to our User class, but this approach comes with some major drawbacks.

Take a moment to reflect: What potential drawbacks might arise from this approach? Jot down your ideas in the widget below.

What potential problems might we face if we choose to add an extra attribute like is_verified directly to our User class to distinguish verified users from regular users?

Clearly, we need a better way to structure our code so that verified users can have their own unique attributes and behaviors without affecting regular users.

Instead of modifying the User class, we’ll create a VerifiedUser class that inherits from User.

Inheritance is an object-oriented programming principle where a new class (the subclass) is derived from an existing class (the parent or base class). The subclass automatically inherits all the attributes and methods of the parent but can also introduce new ones or modify existing behaviors.

  • The subclass inherits all attributes and methods from the parent class.

  • The subclass can introduce new attributes (like a verification badge).

  • The subclass can extend or modify behaviors without affecting the parent class.

It is “IS A” relationship between two classes as:

  • Car IS A Vehicle.

  • Python IS A Programming Language.

  • Circle IS A Shape.

From the above examples of inheritance, we can now say that we can build new classes by extending existing classes.

In Python, whenever we create a class, it is, by default, a subclass of the built-in Python object class. This makes it an excellent example of inheritance in Python. This class has very few properties and methods, but it does provide a strong basis for object-oriented programming in Python.

Now, applying this concept to our project, we’ll create a VerifiedUser class that inherits from User. The VerifiedUser will call the parent constructor using super() to initialize common attributes, and then it will add a new attribute—verification_badge—to indicate that the user is verified.

Note: We don’t change any existing behavior from the User class; we simply extend it by calling the parent’s constructor using super() and then adding an attribute, verification_badge, set to True.

VerifiedUser class

First of all, we’ll create a new class as VerifiedUser.

class VerifiedUser(User):
def __init__(self, username, display_name, password):
# Inherit all attributes from User
super().__init__(username, display_name, password)
# Add a new attribute specific to verified users
self.verification_badge = True
# New method to return verification status
def get_verification_status(self):
return "Verified ✅" if self.verification_badge else "Regular"
VerifiedUser class

If we look into line 1, we’ll see that User is passed inside parentheses as class VerifiedUser(User):. This indicates that the new class, which in our case is VerifiedUser is inherited from the User class.

super() function

The use of super() comes into play when we implement inheritance. It is used in a child class to refer to the parent class without explicitly naming it. It makes the code more manageable, and there is no need to know the name of the parent class to access its attributes.

In the VerifiedUser class, line 4 is a super function that calls the __init__ method of the parent (User) class inherits all the attributes from it (User).

VerifiedUser class inheriting from User class showing the inherited attributes and methods while having its own attributes and methods.
VerifiedUser class inheriting from User class showing the inherited attributes and methods while having its own attributes and methods.

Now, let’s test it.

from user import User

class VerifiedUser(User):
    def __init__(self, username, display_name, password):
        # Inherit all attributes from User
        super().__init__(username, display_name, password)
        # Add a new attribute specific to verified users
        self.verification_badge = True

    # New method to return verification status
    def get_verification_status(self):
        return "Verified ✅" if self.verification_badge else "Regular"
Inheritance implementation

In the newly added class, we introduce inheritance by creating a VerifiedUser class that extends our existing User class. Instead of rewriting all user-related functionality, VerifiedUser leverages the code already defined in User, while simply adding a new attribute (verification_badge) to indicate verification status and its own new method get_verification_status to return verification status.

Challenge: Implement an AdminUser class

Now that we’ve created a VerifiedUser class using inheritance, let’s take it a step further. Many social networks have administrators who can manage user accounts, delete posts, or even verify other users.

Your task

Create an AdminUser class that inherits from User and adds an additional method called delete_post(self, post). This method should delete a post and print a message indicating that the admin has deleted the specified post.

# Add your code here
Challenge: Implement an AdminUser class

What’s next?

Our verified users now have special badges, clearly distinguishing them from regular users. But there’s still something missing. Right now, all users behave exactly the same way, even though in real-world social networks, verified users often enjoy unique privileges—like increased visibility or special interactions.

For example, shouldn’t a verified user’s chirp appear differently or be prioritized over regular ones? What if we want some actions, such as liking, posting, or commenting, to behave differently depending on the user’s status?

As Chirpy continues to grow, we need a clean and scalable way to customize behaviors for different user types without cluttering our codebase. How can we achieve that?

We’ll explore this exciting challenge in the next lesson.

Happy coding, and see you in the next lesson!