python问题?第二个类定义了privileges_list属性,为什么还会报错?
问题相关代码,请勿粘贴截图
class User:
def __init__(self,first_name,last_name,login_attempts):
self.first_name = first_name
self.last_name = last_name
self.login_attempts = login_attempts
def describe_user(self):
name = self.first_name+self.last_name
print(name)
def greet_user(self):
print('Hello',self.first_name,'.',self.last_name)
def increment_login_attempts(self):
for i in range(1,10,2):
self.login_attempts =int(self.login_attempts) +i
print(self.login_attempts)
def reset_login_attempts(self):
self.login_attempts =0
print(f"重置后:{self.login_attempts}")
class Privileges:
def _init_(self,privileges_list = ['can add post','can delete post','can ban user']):
self.privileges_list = privileges_list
def show_privileges(self):
print(f"your privileges as follows:{self.privileges_list}.")
class Admin(User):
def __init__(self,first_name,last_name,login_attempts):
super().__init__(first_name,last_name,login_attempts)
self.privileges = Privileges() # 将实例用作属性
user = Admin(first_name='zhang',last_name='samsam',login_attempts=10)
user.describe_user()
user.greet_user()
user.privileges.show_privileges()#user先调用Admin中priviles属性再调用show_privieges方法
user.increment_login_attempts()
user.reset_login_attempts()
运行结果及报错内容
D:\Anacoda\pythonw.exe D:/pycharm/pythonProject/main.py
zhangsamsam
Hello zhang . samsam
Traceback (most recent call last):
File "D:/pycharm/pythonProject/main.py", line 32, in
user.privileges.show_privileges()#user先调用Admin中priviles属性再调用show_privieges方法
File "D:/pycharm/pythonProject/main.py", line 22, in show_privileges
print(f"your privileges as follows:{self.privileges_list}.")
AttributeError: 'Privileges' object has no attribute 'privileges_list'