Previous Lesson Current Lesson Next Lesson

Question 1

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

In order initialize a Person with name and age attributes, we should pass in two parameters into our __init__ method, which we have named name and age accordingly. In addition, the __init__ takes self as the first argument, as that is the reference to our Person instance.

Question 2

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def years_until_legal(self):
        return self.age - 21

In this method, there are no parameters besides self (the reference to our Person instance) because the difference is computed using the age attribute of our instance and 21, which is just self.age - 21.