【发布时间】:2017-02-23 06:31:03
【问题描述】:
我对 Python 还很陌生,下面的代码摘自我正在使用的一本书。
它列在下面完全,因为它是在书中编写和解释的,但它会引发以下错误:
TypeError: super() takes at least 1 argument (0 given)
当我尝试给 super 一个参数时,它告诉我它需要是类型。
我已经搜索了多个线程,但还没有运气。
class Car():
"""A simple attempt to represent a car"""
def __init__(self, make, model, year):
"""Initialize attributes to describe a car"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
【问题讨论】:
-
这是有效的 Python 3 代码,因此如果遇到问题,您可能正在使用 Python 2。
-
^ 这是有效的 Python 代码,因此您可能使用的是“以前称为 python 的版本”...
标签: python inheritance init super