Подтвердить что ты не робот

Вызов конструктора родительского класса из дочернего класса в python

Итак, если у меня есть класс:

 class Person(object):
'''A class with several methods that revolve around a person Name and Age.'''

    def __init__(self, name = 'Jane Doe', year = 2012):
        '''The default constructor for the Person class.'''
        self.n = name
        self.y = year

И затем этот подкласс:

 class Instructor(Person):
'''A subclass of the Person class, overloads the constructor with a new parameter.'''
     def __init__(self, name, year, degree):
         Person.__init__(self, name, year)

Я немного потерял, как заставить подкласс вызвать и использовать конструктор родительского класса для name и year, добавив в подкласс новый параметр degree.

4b9b3361

Ответ 1

Python рекомендует использовать super().

Python 2:

super(Instructor, self).__init__(name, year)

Python 3:

super().__init__(name, year)