【发布时间】:2016-07-29 08:07:18
【问题描述】:
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() 至少需要 1 个参数(给定 0)
super() 函数有什么问题?
【问题讨论】:
-
用你的超类的名字替换
super(),即。 e.car。或者如果你想要super,super(electricCar, self).__init__(make, model, year) -
您使用的是什么版本的 Python?您可以在 official docs 中看到
super()语法。对于 Python 2,您需要将子类名称指定为类型参数。对于 Python3,你不需要 -
如果您只是学习语言...为什么要从 6 岁 的 python 版本开始?只需从最新版本开始。只有需要 Python2 以向后兼容旧系统/库的人才能使用 Python2。
标签: python python-2.x