Using instance as an attribute

I have created a parent class called User. Then I have created another class called Privileges which I want to use as an attribute in the child class Admin. I want Admin to have certain privileges but I don’t know where to mention them; for example, can cancel, can ban. Can you tell me what’s the error in my code?

class User():
    def __init__(self,first_name, last_name):

    def name(self):
        return self.first.title() + " "+ self.last.title()


class Privilege():
    def __init__(self,privileges):

    def show_privileges(self):
        print ("\nFollowing are the administrators set of privileges:")
        for privilege in self.privileges:

class Admin(User):
    def __init__(self, first_name,last_name):

admin.admin_privileges.privileges=["can slap","can beat","can jump"]

This means, when we create a Privilege object, we should pass in a value called privileges.

But from the code, it turns out that we will ignore that value and just make an empty list anyway.

This tries to create a Privilege instance, but it doesn’t pass the required value for privileges.

So the design is simply inconsistent. You need to decide how you want it to work, and then make sure everything does it the same way.

This will be fine when other things are fixed; but it will be easier if the Admin class takes in this list as another parameter, and passes it as an argument for creating the Privilege, and then Privilege uses that list instead of making an empty one. (Or maybe it should copy the values into a new list; that’s a useful way to make sure a) the Privilege.privileges attribute is a list; b) that the Privilege object “owns” that list and is completely responsible for updates to the list contents, and won’t be affected by a change somewhere else.) By designing things this way, we make sure that every class is responsible for itself, and is not responsible for others.

This is simply a typo; it should say show_privileges instead of show.privileges.

1 Like