user.py
class User(): def __init__(self,first,last,age,class_numberr = 3,login_attempts = 1024): self.first_name = first self.last_name = last self.age = age self.class_number = class_numberr self.login_attempts = login_attempts def print_name(self): print("Name is:" + self.first_name.title() + " " + self.last_name.title()) def greeting(self): print("hello") ## print("Hello," + self.first_name + " " + self.last_name) def class_is(self): print("Class is:" + str(self.class_number)) def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0admin.py
from user import User class Admin(User): def __init__(self,f,l,a,privileges = ['can add post','can delete post','can ban user']): super().__init__(self,f,l,a) #这里多个self,会出现不可预料的错误 self.privileges = privileges def show_privileges(self): for privilege in self.privileges: print(" -privilege") a1 = Admin('robert','jone',12) a1.greeting() a1.print_name() a1.class_is() a1.increment_login_attempts() a1.reset_login_attempts()编译admin.py后,发现如下错误:
r@r:~/coml/python/9/93/2$ python3 admin.py hello Traceback (most recent call last): File "admin.py", line 11, in <module> a1.print_name() File "/home/r/coml/python/9/93/2/user.py", line 9, in print_name print("Name is:" + self.first_name.title() + " " + self.last_name.title()) AttributeError: 'Admin' object has no attribute 'title'如果去掉.title()后,
r@r:~/coml/python/9/93/2$ python3 admin.py hello Traceback (most recent call last): File "admin.py", line 11, in <module> a1.print_name() File "/home/r/coml/python/9/93/2/user.py", line 9, in print_name print("Name is:" + self.first_name + " " + self.last_name) TypeError: must be str, not Admin r@r:~/coml/python/9/93/2$这个不是user.py的问题,而是admin中出现了问题