码迷,mamicode.com
首页 > 编程语言 > 详细

记录Python类与继承的一个错误

时间:2018-11-24 14:38:33      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:class   his   原来   ret   错误   pytho   car   ctrl+v   +=   

今天在学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()那里少了一个括号。

 

记录Python类与继承的一个错误

标签:class   his   原来   ret   错误   pytho   car   ctrl+v   +=   

原文地址:https://www.cnblogs.com/Guhongying/p/10011389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!