Why did this error appear? "TypeError: descriptor '__init__' requires a 'super' object but received a 'str'" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why did this error appear? "TypeError: descriptor '__init__' requires a 'super' object but received a 'str'"

class Car(): def __init__(self, modalname, yearm, price): self.modalname = modalname; self.yearm = yearm self.price = price def price_inc(self): self.price = int(self.price * 0.2) class SuperCar(Car): def __init__(self, modalname, yearm, price, cc): super.__init__(modalname, yearm, price) print(self.cc) honda = SuperCar("City", 2017, 10000,cc=1500) Toyota = Car("Allion", 2022, 3000) honda.cc = "1500" honda.price_inc() print(honda.price)

4th Jun 2021, 4:48 PM
Nafi Rahat Rahman
2 Answers
0
Well you don't need parenthesis after the class name "Car" if you put parenthesis then you are telling the code the class "Car" wants to derive another class but you're not giving it the name of the class that's why the error came . Now i will explain what does the error mean, a derived class is called as "parent class" or "super class" and the class deriving it is called as "child class" or "sub class" that's why the error "super object requires" exits because you're making an object of the class "Car" but you are giving a string "Allion" that's why this error came "but received a str"
4th Jun 2021, 4:54 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
class Car: def __init__(self, modalname, yearm, price): self.modalname = modalname; self.yearm = yearm self.price = price def price_inc(self): self.price = int(self.price * 0.2) class SuperCar(Car): def __init__(self, modalname, yearm, price, cc): super.__init__(modalname, yearm, price) print(self.cc) honda = SuperCar("City", 2017, 10000,cc=1500) Toyota = Car("Allion", 2022, 3000) honda.cc = "1500" honda.price_inc() print(honda.price)
4th Jun 2021, 5:00 PM
Eashan Morajkar
Eashan Morajkar - avatar