Guys, I'm trying to figure out how to inherit, and I can't figure out how to override the get_extra_mony() method from the parent Student class in the inherited Warden() class.

What needs to be done to add a simple print() to this method? I tried to start by redefining __init__ in the Warden class like this: Student.__init__(self,extra_mony) .

Paycharm extra_mony underscores the red variable and writes the error AttributeError: 'Warden' object has no attribute 'extra_mony'. In other sources, they write that you need to use the super() method.

 class Student(): def __init__(self,extra_mony): self.extra_mony = extra_mony self.tax = 5 def set_extra_mony(self,extra_mony): self.extra_mony = extra_mony def set_tax(self,tax): self.tax = tax def get_tax(self): return self.tax def get_extra_mony(self): my_tax = self.extra_mony / 100 finnaly_tax = my_tax*self.tax print('cтепендия минус налог:',self.extra_mony- finnaly_tax,'грн') return self.extra_mony - finnaly_tax class Warden(Student): def __init__(self,count_people): self.count_people = count_people Student.__init__(self,extra_mony) 
  • "Mony" - exactly what you mean? - Don2Quixote
  • probably all the same 'many')) - Jeniaκv
  • one
    @Jeniakv, I, of course, not a polyglot, but, probably, still "money". =) - Don2Quixote
  • one
    Well, let's start with the fact that in your Warden there really is nowhere to take the variable extra_mony - andreymal

1 answer 1

Threw an example, try:

 class Student: def __init__(self,extra_mony): self.extra_mony = extra_mony self.tax = 5 def set_extra_mony(self,extra_mony): self.extra_mony = extra_mony def set_tax(self,tax): self.tax = tax def get_tax(self): return self.tax def get_extra_mony(self): my_tax = self.extra_mony / 100 finnaly_tax = my_tax*self.tax print('cтепендия минус налог:',self.extra_mony- finnaly_tax,'грн') return self.extra_mony - finnaly_tax class Warden(Student): # В конструкторе Student передается extra_mony def __init__(self, count_people, extra_mony): super().__init__(extra_mony) self.count_people = count_people # Переопределенный метод def get_extra_mony(self): # Вызов родительского метода value = super().get_extra_mony() print("get_extra_mony ->", value) return value