'attributeError: 'list' object has no attribute.....

I am getting attributeError, but I don't understand....

class User():

    def __init__(self, first, last, age):
        self.first = first
        self.last = last
        self.age = age
        self.login_attempt = 0

class Admin(User):

    def __init__(self, first, last, age):
        super().__init__(first, last, age)
        self.privilages = Privilages()


class Privilages():

    def __init__(self, privilages = ''):
        self.privilages = []

    def show_privilages(self):
        print("There are the privilages... : ")
        if self.privilages:
            for privilage in self.privilages:
                print("- " + privilage)
        else:
             print("The user has no privilages. ")


sarah.privilages = ['can add post', 'can delete post']
sarah.privilages.show_privilages()

I am not sure what I am missing here, I used for loops to go over the list and print it out, however I keep getting error of "'list' object has no attribute 'show_privileges'"



Solution 1:[1]

You're assigning a list to sarah.privilages, so it surely does not have a show_privilages method. You should make the __init__ method of Admin take a list of privileges as a parameter, so it can pass on to the __init__ method of Privilages to initialize its privilages attribute:

class Admin(User):
    def __init__(self, first, last, age, privilages):
        super().__init__(first, last, age)
        self.privilages = Privilages(privilages)


class Privilages():
    def __init__(self, privilages):
        self.privilages = privilages

    def show_privilages(self):
        print("There are the privilages... : ")
        if self.privilages:
            for privilage in self.privilages:
                print("- " + privilage)
        else:
             print("The user has no privilages. ")

sarah = Admin('sarah','mary','smith', ['can add post', 'can delete post'])
sarah.privilages.show_privilages()

This outputs:

There are the privilages... : 
- can add post
- can delete post

Solution 2:[2]

Write a separate Privileges class. The class should have one attribute, privileges, that stores a list of strings. Move the show_privileges() method to this class. Make a Privileges instance as an attribute in the Admin class. Create a new instance of Admin and use your method to show its privileges

class User():
    """Represent a simple user profile."""

    def __init__(self, first_name, last_name, username, email, location):
        """Initialize the user."""
        self.first_name = first_name.title()
        self.last_name = last_name.title()
        self.username = username
        self.email = email
        self.location = location.title()

    class Admin(User):
    def __init__(self, first_name, last_name, username, email, location):
        super().__init__(first_name, last_name, username, email, location)
        #Initialize an empty set of privileges.
        self.privileges= Privileges()

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

    def Show_privileges(self):
        print("\nPrivileges: ")
        if self.privileges:
            for privilege in self.privileges:
                print("- " + str(privilege))
        else:
            print("- this user has no privileges.")

        eric = Admin('suraj', 'boi', 'e_mater', '[email protected]', 'alaska')
    eric.describe_user()
    
    eric.privileges.Show_privileges()
    print("\nAdding privileges...")
    eric_privileges = [
        'can reset passwords',
        'can moderate discussions',
        'can suspend accounts',
        ]
    
    eric.privileges.privileges = eric_privileges
    eric.privileges.Show_privileges()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blhsing
Solution 2 suraj_0o0