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分別給予
'User .__ init__'接受'first_name'和'last_name'參數,但是你也傳遞'privileges' –
所以我不應該傳遞特權? –
我回答了我自己的問題。謝謝你的幫助! –