class Car:
def __init__(self,make,model):
self.make =make
self.model = model
def __repr__(self):
return f'<car {self.make} {self.model}>'
class Garage:
def __init__(self):
self.cars = []
def __len__(self):
return len(self.cars)
def add_car(self,car):
if not isinstance(car,Car):# accept only clss obcject clas car
raise TypeError(f'Tried to add `car.__class__.__name__ to the garage but you can only add car object')
self.cars.append(car)
#raise NotImplemented('We cant add catss to the garage yet')# you create a new error of type notimplemented error
ford =Garage()
car = Car('Ford','Fiesta')
ford.add_car(car)
print(len(ford))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter