Implement the Fruit class with the size, weight (numeric values) and taste (string) variables, inherit the apple classes with the new color variable (string value) and orange with the color variable, but without taste. Put two apples and an orange in FruitBasket.

The question is, if I inherit Orange from Fruit, how can I not inherit from a variable taste?

So does not pass, writes that 1 argument is lost.

class Fruit: def __init__(self, size, weight, taste): self.size = size self.weight = weight self._taste = taste class Apple(Fruit): def __init__(self, size, weight, _taste, color): super().__init__(size, weight, _taste) self.color = color class Orange(Fruit): def __init__(self, size, weight, color): super().__init__(size, weight) self.color = color first_apple = Apple(1, 5, 'Сочное', 'Красное') second_apple = Apple(2, 7, 'Кислое', 'Зеленое') orange = Orange(3, 8, 'Оранжевый') 

    2 answers 2

    You inherit from "fruit." This means all the attributes of the fruit will be transferred to the descendant ("apple", "orange"). I see several options:

    • form a parent class without taste, descendant with taste, and then the next descendant "apple"
    • make the parent class more flexible - for example, assign a taste - as the default parameter, then it can be specified for "apple" and not specified for "orange"

       class Fruit: def __init__(self, size, weight, taste=""): self.size = size self.weight = weight self.taste = taste second_apple = Apple(2, 7, 'Кислое', 'Зеленое') orange = Orange(3, 8, 'Оранжевый') 
    • or discard variables and make an attribute dictionary in the main parent without the scope of the names, etc. Then, when inheriting, all parameters will be stored in it (the code of the following example was tested in python 2):

       class Fruit1: def __init__(self, **fruit_dict): self.attr = fruit_dict class Apple1(Fruit1): def __init__(self, **fruit_dict): Fruit1.__init__(self, **fruit_dict) first_apple1 = Apple1(**{"size": 3, "weight": 5, "taste": 'Сочное', "color": 'Красное'}) orange1 = Orange1(**{"size": 3, "weight": 5, "color": 'Оранжевый'}) print(first_apple1.attr) print(orange1.attr) 

      If I understand the question correctly, then you cannot selectively pass arbitrary arguments to the subclass by definition. But you can selectively pick them up in a subclass if you leave some leeway in the parent class.

       class Fruit: def __init__(self, size, weight, taste, *args, **kwargs): self.size = size self.weight = weight self._taste = taste class Apple(Fruit): def __init__(self, size, weight, _taste, color): super().__init__(size, weight, _taste) self.color = color class Orange(Fruit): def __init__(self, size, weight, color, *args, **kwargs): super().__init__(size, weight, color) self.color = color first_apple = Apple(1, 5, 'Сочное', 'Красное') second_apple = Apple(2, 7, 'Кислое', 'Зеленое') orange = Orange(3, 8, 'Оранжевый') 

      And then:

       print(orange.weight) print(second_apple._taste) 

      The output will give:

       8 Кислое