How do I do this?

class Car:
def init(self,model,mileage):
self.model=model
self.mileage=mileage
def str(self):
return “{} gives {}”.format(self.model,self.mileage)
def len(self):
return

How do I define len such that later when I do len(obj) it returns the length of the string produced by str(obj)?

hey @Nitin-Bhattacharyya-2319140631658033 ,
first thing __len__ is meant for use when you are iterating over a generator, because at that time you dont know its actual length.

and no coming to your doubt.
if you still want to do it in __len__ ( although it wont do anything )
do it like
def __len__(self):
return len(self.__str__())

but more better it would be to just do a call like ( just a sample. )
m = Car("Toyota","55km/h")
print(len(m))

I hope this helps. :slightly_smiling_face:.

Thanks it helped. I understand the problem now.

Thats good to hear that.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.