我正在玩python类继承,但我似乎无法让子类工作。
我在这里收到的错误消息是:
must be a type not classobj
以下是代码:
class User():
"""Creates a user class and stores info"""
def __init__(self, first_name, last_name, age, nickname):
self.name = first_name
self.surname = last_name
self.age = age
self.nickname = nickname
self.login_attempts = 0
def describe_user(self):
print("The users name is " + self.name.title() + " " + self.surname.title() + "!")
print("He is " + str(self.age) + " year's old!")
print("He uses the name " + self.nickname.title() + "!")
def greet_user(self):
print("Hello " + self.nickname.title() + "!")
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
"""This is just a specific user"""
def __init__(self, first_name, last_name, age, nickname):
"""Initialize the atributes of a parent class"""
super(Admin, self).__init__(first_name, last_name, age, nickname)
jack = Admin('Jack', 'Sparrow', 35, 'captain js')
jack.describe_user()
我使用的是Python 2.7
答案 0 :(得分:2)
User
课程如果您稍后打电话给object
,则必须继承super()
。
class User(object):
...
Python2区分了未从object
继承的旧式类和新的样式类。最好在现代代码中使用新的样式类,并且永远不能将它们混合在继承层次结构中。