100 days of code: Day 31 - override parent

Last modified on 2020-07-09

Source: 100-days-of-code.

Override any method in the users.py parent class by defining a new method in the child class with the same name and different code …

class User:
    """A simple attempt to represent a user."""

[...]

    def greet_user(self):
        """A personalized greeting for a user."""
        play = input(f"Hello {self.first_name}! "
                + "Would you like to play a game? Y/N > ")
        if play.lower() == 'y':
            # there is no 'correct' login, its just playing out a loop
            while self.login_attempts < self.max_login_attempts:
                login = input("Enter login: ")
                print(f"Login {login} is incorrect.")
                self.increment_login_attempts()
                print(f"Login attempts: {self.login_attempts}")
            print("Maximum login attempt limit reached. No game for you. :-(")
        else:
            print("OK. No game.")

[..]

class Superuser(User):
    """Represent special privilegs for a system's superuser."""


    def __init__(self, first_name, last_name, city, country):
        """Initialize attributes of the parent class."""
        super().__init__(first_name, last_name, city, country)
        self.defcon_level = 5


    def greet_user(self):
        """No login necessary for superuser."""
        print("Retina and fingerprint scan complete. Access granted.")

Happy hacking!