python class 继承问题

Problems with python class inheritance

我正在研究 python class 继承,但我似乎无法让 child class 工作。

我收到的错误消息是:

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

User class 必须继承自 object 如果您稍后要调用 super()

class User(object):

...

Python2 区分未继承自 object 的旧式 class 和继承自 class 的新式 class。最好在现代代码中使用新样式 classes,在继承层次结构中混用它们绝不好。