簡體   English   中英

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()至少接受1個參數(給定0)

super()函數有什么問題?

python3中引入了super() (不帶參數),這是python2的實現。

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

您可以參考此問題以獲取有關python2python3的一般繼承語法問題

似乎您正在嘗試使用Python 3語法,但是您正在使用Python2。在該版本中,您需要將當前類和實例作為參數傳遞給super函數:

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

如果您使用的是python 2,則需要使用super方法顯式傳遞實例。 在python 3或更高版本中,實例變量是隱式傳遞的,您無需指定它。 self是這car一個實例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM