麻烦使用Python超级

时间:2017-06-23 18:41:09

标签: python super

所以我目前正在使用Python Crash Course这本书学习Python的基础知识。 我关于类的章节,更确切地说是关于类的子类。 首先,这是我的代码:

class Car():
"""une representation simpliste de voiture"""

def __init__(self, constructeur, annee, modele):
    self.constructeur = constructeur
    self.modele = modele
    self.annee = annee
    self.odometer_reading = 0

def descriptive_name(self):
    long_name = str(self.annee) + ' ' + self.constructeur + ' ' + self.modele
    return long_name.title()

def update_odometer(self, mileage):
    """set odometer reading"""

    if mileage >= self.odometer_reading:
        self.odometer_reading = mileage
    else:
        print("You can't roll back and odometer!\n")

def increment_odometer(self, miles):
    self.odometer_reading += miles

def read_odometer(self):
    """print mileage"""
    print("this car has " + str(self.odometer_reading) + " miles on it.\n")
class ElectricCar(Car):
def __init__(self, constructeur, annee, modele):
    super().__init__(constructeur, annee, modele)
    pass
my_tesla = ElectricCar('tesla', 'model s', 2016)

因此,使用此代码我收到此错误消息:

  

super()。 init (constructeur,annee,modele)   TypeError:super()至少需要1个参数(给定0)

我使用的代码与书中的代码相同(法语部分除外,书中的英文版)。 我试过和没有"传递" " super()"并尝试给予超级"争论"自我"和" Car"。

我在Linux上使用Python 3和我。

提前感谢您的回答:)

1 个答案:

答案 0 :(得分:2)

super()语法是Python 3 如果您仍在使用Python 2,则需要super(ElectricCar,self)