I start learning OOP in Python. I created the Buildings class, a subclass of Market in which I inherited all the attributes from Buildings. I try to create an object of a subclass, but I get an error.

AttributeError: 'str' object has no attribute '_Building__material'

class Building: def __init__(self, material, color, number=0): self.__material = material self.__color = color self._number = number self.place(number) def place(self, number): if self._number <= 0: print("Out of stock") elif (self._number > 0) and (self._number < 100): print("Placed in Warehouse") else: print("Remote warehouse") def get_material(self): return self.__material def set_material(self, material): self.__material = material def get_color(self): return self.__color def set_color(self, color): self.__color = color def get_number(self): return self._number def set_number(self, number): self._number = number material = property(get_material, set_material) color = property(get_color, set_color) number = property(get_number, set_number) def plus(self, quantity): self._number += quantity self.place(self._number) print("Added {} things of material".format(quantity)) def minus(self, quantity): self._number = self._number - quantity self.place(self._number) print("Removed {} things of material".format(quantity)) def __str__(self): return 'Building:\n\tmaterial: {}\n\tcolor: {}\n\tnumber: {}\n'\ .format(self.__material, self.__color, self._number) class Market(Building): def __init__(self, material, color, number=0, price=0): Building.__init__(material, color, number) self._price = price def get_price(self): return self._price def set_price(self, price): self._price = price price = property(get_price, set_price) def plus(self, quantity): super()._number += quantity super().place(super()._number) print("Added {} things of material".format(quantity)) bricks_market = Market("bricks", "orange", 120, 200) 

    2 answers 2

    The first argument to the method is self , and you pass the material line to Building.__init__ . Use super().__init__(**kwargs) instead .

      Replace the line:

       Building.__init__(material, color, number) 

      on this:

       Building.__init__(self, material, color, number)