Question

TypeError: Privileges.show_privileges() missing 1 required positional argument: 'self'

I'm a beginner in python and I'm using python crash course 2019 as my reference. I'm having difficulty figuring out what is wrong with my code gaining the type error as said in the title. here is my line of code:

class Users:
    def __init__(self, first_name, last_name, email, username):
        self.first_name = first_name.title()
        self.last_name = last_name.title()
        self.email = email
        self.username = username

    def describe_user(self):
        """summary of user's information"""
        print(f"Name: {self.first_name} {self.last_name}")
        print(f"Email: {self.email}")
        print(f"Username: {self.username}")

    def greet_user(self):
        """personalized greeting to the user"""
        print(f"Welcome back, {self.username}\n")


class Admin(Users):
    def __init__(self, first_name, last_name, email, username):
        super().__init__(first_name, last_name, email, username)
        self.privileges = Privileges


class Privileges:
    def __init__(self, privileges=[]):
        self.privileges = privileges

    def show_privileges(self):
        print("These are the administrator's privileges:")
        for privilege in self.privileges:
            print(f" -{privilege}")


admin = Admin('emmanuel', 'mangubat', 'emmanuelmangubat21@gmail.com', 'that3pletho')
admin.describe_user()
admin.greet_user()
admin.privileges.show_privileges()
 3  58  3
1 Jan 1970

Solution

 4

The error is this line in your Admin constructor:

self.privileges = Privileges

Since you didn't add parentheses after "Privileges", this line refers to the Privileges class, not a Privileges instance.

Adding in the parentheses will initialize a new Privileges object for each Admin instance instead of referring to the class as a whole:

class Admin(Users):
    def __init__(self, first_name, last_name, email, username):
        super().__init__(first_name, last_name, email, username)
        self.privileges = Privileges()
2024-07-10
Andrew Yim