我收到attributeError,但是我不明白。...
class User():
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
self.login_attempt = 0
class Admin(User):
def __init__(self, first, last, age):
super().__init__(first, last, age)
self.privilages = Privilages()
class Privilages():
def __init__(self, privilages = ''):
self.privilages = []
def show_privilages(self):
print("There are the privilages... : ")
if self.privilages:
for privilage in self.privilages:
print("- " + privilage)
else:
print("The user has no privilages. ")
sarah.privilages = ['can add post', 'can delete post']
sarah.privilages.show_privilages()
我不确定我在这里缺少什么,我使用循环遍历列表并将其打印出来,但是我不断收到“'list'对象没有属性'show_privileges'”的错误
答案 0 :(得分:1)
您正在为sarah.privilages
分配一个列表,因此它肯定没有show_privilages
方法。您应该使__init__
的{{1}}方法以特权列表作为参数,以便它可以传递给Admin
的{{1}}方法以初始化其{{1 }}属性:
__init__
这将输出:
Privilages