記錄Python類與繼承的一個錯誤
阿新 • • 發佈:2018-11-24
今天在學python的類與繼承的時候遇到一個錯誤,原來是自己在ctrl+c ctrl+v的時候漏了一個括號
1 class Car(): 2 def __init__(self,make,year,model): 3 self.make=make 4 self.model=model 5 self.year=year 6 self.odometer_reading=0 7 def get_descriptive_name(self): 8 long_name=str(self.year)+""+self.make+" "+self.model 9 return long_name.title() 10 def read_odometer(self): 11 print("This car has "+str(self.odometer_reading)+"miles on it") 12 13 def update_odometer(self,mileage): 14 if mileage>=self.odometer_reading: 15 self.odometer_reading=mileage16 else: 17 print("You can't roll back an odometer") 18 def increment_odometer(self,miles): 19 self.odometer_reading+=miles 20 class ElectricCar(Car): 21 def __init__(self,make,model,year): 22 super.__init__(make,year,model) 23 my_tesla=ElectricCar('tesla','model s',2016) 24 print(my_tesla.get_descriptive_name())
執行時出現了以下的錯誤
super.__init__(make,year,model)
TypeError: descriptor '__init__' requires a 'super' object but received a 'str'
原來是22行的super()那裡少了一個括號。