2016-07-29 44 views
0
class car(object): 

    def __init__(self, make, model, year): 
     self.make = make 
     self.model = model 
     self.year = year 
     self.odometer_reading = 0 

class electricCar(car): 
    def __init__(self, make, model, year): 
     super().__init__(make, model, year) 

tesla = electricCar('tesla', 'model s', 2016) 
print tesla.get_descriptive_name() 

TypeError: super() takes at least 1 argument (0 given)python super()函数错误?

super()函数有什么问题?

+0

用你的超类名称i替换'super()'。即'car'。或者如果你想'super','super(electricCar,self).__ init __(make,model,year)' – BusyAnt

+0

你使用的是什么版本的Python?你可以在[官方文档](https://docs.python.org/2/library/functions.html#super)中看到'super()'语法。对于Python 2,您需要指定子类名称作为类型参数。对于Python3,你不需要 – RedBaron

+0

如果你只是在学习语言......你为什么从一个* 6岁*的Python版本开始?刚开始使用最新版本。 Python2只能由需要它的用户使用,以便与旧系统/库向后兼容。 – Bakuriu

回答

7

super()(不带参数)在python3 介绍这里是python2实施。

class electricCar(car): 
    def __init__(self, make, model, year): 
     super(electricCar,self).__init__(make, model, year) 

你可以参考this question一般继承语法问题有关python2python3

3

它看起来像你试图使用Python 3语法,但你使用Python 2.在版本,你需要将当前类和实例作为参数传递给super功能:

super(electricCar, self).__init__(make, model, year) 
+0

谢谢大家的帮助! – npkp

0

如果你是USI ng python 2你需要使用super方法显式地传递你的实例。在python 3或更高版本中,实例变量是隐式传递的,你不需要指定它。这里self是这个类的一个实例car

super(car, self).__init__(make, model, year)