2017-02-25 52 views
1

我正在嘗試使用下面的代碼創建一個單獨的Privilege類,該類有一個名爲show_Privileges的方法。該方法打印用戶權限。在python中調用另一個類的方法

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

當試圖運行時,我得到以下錯誤。有任何想法嗎?

回溯(最近通話最後一個):

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) 

類型錯誤:初始化()接受,但4分別給予

+2

'User .__ init__'接受'first_name'和'last_name'參數,但是你也傳遞'privileges' –

+0

所以我不應該傳遞特權? –

+0

我回答了我自己的問題。謝謝你的幫助! –

回答

0

User__init__接受3個參數即self, first_name, last_name但你是3的位置參數通過4這裏,privileges是額外的。

如果您尚未登錄,請參閱standard docs on super

+0

是的,我對於超級作品有些困惑,但刪除了privleges讓我的代碼按照它的設想工作。謝謝你的幫助! –