python super()函数错误?
python super() function error?
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)
super() 函数有什么问题?
super()
(无参数)在 python3 中引入
这是 python2 实现。
class electricCar(car):
def __init__(self, make, model, year):
super(electricCar,self).__init__(make, model, year)
关于 python2 和 python3 的一般继承语法问题,您可以参考 this question
看起来您正在尝试使用 Python 3 语法,但您使用的是 Python 2。在该版本中,您需要将当前的 class 和实例作为super
函数的参数:
super(electricCar, self).__init__(make, model, year)
如果您使用的是 python 2,则需要使用 super 方法显式传递您的实例。在 python 3 或更高版本中,实例变量是隐式传递的,您不需要指定它。这里 self
是 class car
的一个实例
super(car, self).__init__(make, model, year)
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)
super() 函数有什么问题?
super()
(无参数)在 python3 中引入
这是 python2 实现。
class electricCar(car):
def __init__(self, make, model, year):
super(electricCar,self).__init__(make, model, year)
关于 python2 和 python3 的一般继承语法问题,您可以参考 this question
看起来您正在尝试使用 Python 3 语法,但您使用的是 Python 2。在该版本中,您需要将当前的 class 和实例作为super
函数的参数:
super(electricCar, self).__init__(make, model, year)
如果您使用的是 python 2,则需要使用 super 方法显式传递您的实例。在 python 3 或更高版本中,实例变量是隐式传递的,您不需要指定它。这里 self
是 class car
super(car, self).__init__(make, model, year)