D.Wes
D.Wes

Reputation: 27

Calling on a method from another class in python

What I am trying to do with the code below is to create a separate Privilege class that has a method called show_Privileges. The method prints a user rights.

class User:
    """Describes users of a system"""
    def __init__(self, first_name, last_name):
        """Initialize first name and last name"""
        self.first_name = first_name
        self.last_name = last_name
        self.login_attempts = 0

    def describe_user(self):
        """Describing the user"""
        print("User " + self.first_name.title() + " " + self.last_name.title() + ".")

    def greet_user(self):
        """Greeting"""
        print("Hello " + self.first_name.title() + " " + self.last_name.title() + "!")

    def increment_login_attempts(self):
        """Increments the number of login attempts by 1"""
        self.login_attempts += 1
        print("User has logged in " + str(self.login_attempts) + " times.")

    def reset_login_attempts(self):
        self.login_attempts = 0
        print("User has logged in " + str(self.login_attempts) + " times.")

class Privileges:
    """Making a privileges class"""
    def __init(self, privileges):
        """Defining the privilege attribute"""
        self.privileges = privileges

    def show_privileges(self):
        """Defining privileges"""
        rights = ['can add post', 'can delete post', 'can ban user']
        print("The user has the following rights: \n")
        for right in rights:
            print(right.title())

class Admin(User):
    """Describes admin rights"""
    def __init__(self, first_name, last_name, privileges):
        """Initialize first and last name"""

        super().__init__(first_name, last_name, privileges)
        self.privileges = Privileges()




super_Admin = Admin('Ed', 'Ward', 'root')
print(super_Admin.describe_user())
super_Admin.privileges.show_privileges()

when attempting to run, I get the following error. Any ideas?

Traceback (most recent call last):

 File "users.py", line 50, in <module>
    super_Admin = Admin('Ed', 'Ward', 'root')
  File "users.py", line 44, in __init__
    super().__init__(first_name, last_name, privileges)

TypeError: init() takes 3 positional arguments but 4 were given

Upvotes: 1

Views: 67

Answers (1)

harshil9968
harshil9968

Reputation: 3244

__init__ of User accept 3 arguments namely self, first_name, last_name but you're passing 4 here, privileges being the extra one.

See the standard docs on super if you haven't already.

Upvotes: 0

Related Questions