1
class Car:

    def __init__(self, brand, color, model, year, mileage):
        self.brand = brand
        self.color = color
        self.year = year
        self.model = model
        self.mileage = mileage

    def __str__(self):
        return  f'TYPE: NORMAL CAR\nBrand: {self.brand}\nColor: {self.color}\n'

    def __add__(self, other):
        line = '- ' * 15
        return '\n'.join([str(self), self.documentation(), line, str(other), other.documentation(), line])

    def documentation(self):
        return f'DOCUMENTATION\nModel: {self.model}\nYear: {self.year}\nMileage: {self.mileage} Km'


class SportCar(Car):

    def __init__(self, brand, color, model, year, mileage, engine, maxspeed):
        super().__init__(brand, color, model, year, mileage)
        self.engine = engine
        self.maxspeed = maxspeed

    def __str__(self):
        return  f'TYPE: SPORT CAR\nBrand: {self.brand}\nColor: {self.color}\n'

    def documentation(self):
        return f'DOCUMENTATION\nModel: {self.model}\nYear: {self.year}\nMileage: {self.mileage} Km\n' \
           f'Engine: {self.engine}\nMax Speed: {self.maxspeed}'


car1 = SportCar('Audi', 'Black', 'R8', '2018', '35 000', 'V6', '350 Km/h')
car2 = Car('Skoda', 'White', 'Superb', '2015', '165 000')
car3 = Car('Honda', 'Blue', 'Civic', '2017', '220 000')
car4 = SportCar('BMW', 'Red', 'M5', '2020', '10 000', 'V8', '430 Km/h')

print(car1 + car2 + car3 + car4)

I am trying to sum up all 4 instances to show their details but it doesn't work I know if I print(car1 + car2) and under it print(car3 + car4) will result in the thing I want but I was just curious how to make the code to be able to run it like above, all 4 summed up and eventually everything I keep adding to work fine Also print(str(car1 + car2) + '\n' + str(car3 + car4)) works too but still.. Again, I am quiet new to python and it's also my first language, thanks :v

Vesedon
  • 13
  • 4
  • 2
    Does this answer your question? [Using \_\_add\_\_ operator with multiple arguments in Python](https://stackoverflow.com/questions/46885618/using-add-operator-with-multiple-arguments-in-python) – pypae Sep 16 '21 at 14:46
  • 2
    `+` is a binary operator (there's a unary `+` as well but that is unrelated). So, there will be exactly two operants: `self` and `other`. – Klaus D. Sep 16 '21 at 14:49
  • 1
    This doesn't work because the first part of the addition `car1 + car2` produces a string, which then can't be added to another Car instance. – John Gordon Sep 16 '21 at 14:57

1 Answers1

2

Your __add__ function is returning a str. Think of this line print(car1 + car2 + car3 + car4) as doing:

temp = car1 + car2 # car + car = str
temp1 = temp + car3 # str + car = error

Typically when you're adding 4 objects together, you're implicitly doing (((a+b)+c)+d) rather than a single operation of a+b+c+d.

To get around this, you need to make your __add__ function return a Car object. There's a variety of ways you can make your function do this, so I'll leave that to you.

Alternatively, you can also skip using the __add__ function and make a separate function which accepts a tuple of Cars and adds them there.

  • 1
    Couldn't ```str + car``` be supported by adding an ```__radd__()``` method to ```Car``` ? – sj95126 Sep 16 '21 at 14:51
  • That works too. – BlueBuffalo73 Sep 16 '21 at 14:53
  • 2
    Yes, but `car + car` returning a string in the first place is deeply counterintuitive, so I like that this answer explains both what the OP *wants* to do and what the OP *should* be doing. – Silvio Mayolo Sep 16 '21 at 14:54
  • Conversely, you could argue that adding two cars together makes no sense whatsoever. – sj95126 Sep 16 '21 at 15:10
  • 1
    I ended up using a function just like you said! `def car_showcase(): show = (car1, car2, car3, car4) line = '- ' * 15 for x in show: print(x) print(line) car_showcase()` – Vesedon Sep 16 '21 at 17:12