1

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'"

4
  • 2
    You do sarah.privilages = [] on the line above. sarah.privilages is then a list. What is your intent? Did you mean sarah.privilages.privilages = [...]? Commented Nov 29, 2018 at 19:39
  • 1
    Not the cause of your error, but "privilege" is spelled with an "e". Commented Nov 29, 2018 at 19:44
  • Oops! I haven't much paid attention on spelling. Anyway, I did sarah.privilages = [] because I wanted to put values in self.privilages = []. Can you explain why it's sarah.privilages.privilages = []? Commented Nov 29, 2018 at 20:33
  • @Sarah Because you start with the Admin class object, then you access it's attribute 'privileges' which is the Privileges class, and then from within that class you access show_privileges Commented Apr 2, 2021 at 23:26

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.