I have a problem with overloading adding operator in Python. Everytime I try to overload it i get:
TypeError: __init__() takes exactly 3 arguments (2 given)
Here is my code:
class Car:
carCount = 0
def __init__(self,brand,cost):
self.brand = brand
self.cost = cost
Car.carCount +=1
def displayCount(self):
print "Number of cars: %d" % Car.carCount
def __str__(self):
return 'Brand: %r Cost: %d' % (self.brand, self.cost)
def __del__(self):
class_name = self.__class__.__name__
print class_name,'destroyed'
def __add__(self,other):
return Car(self.cost+other.cost)
c=Car("Honda",10000)
d=Car("BMW",20000)
print c
a= c+d
return Car(self.cost+other.cost). you are missing brand but I imagine you wantreturn self.cost+other.costbrand?