记录Python类与继承的一个错误
今天在学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=mileage 16 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()那里少了一个括号。