Python 3:类型错误对象.__ new __()不带参数

时间:2014-11-18 01:24:33

标签: class python-3.x typeerror

我在开始上课时遇到了问题。我收到此类型错误,但我不确定是什么造成了这种错误。我检查了其他问题,但他们的答案似乎并不能解决我的问题。

#create the car class
class Car:

    #create the initiator function
    def __init__carStats(self, year, model):
        self.__speed = 0
        self.__year = year
        self.__model = model

    def setModel(self, model):
        self.__model = model

    def setYear(self, year):
        self.__year = year

    #create speed increase function
    def speedUp(self):
        print('Calling speed up function.')
        self.__speed += 5

    #create slowdown function
    def slowDown(self):
        print('Calling slow down function.')
        self.__speed -= 5

    #create show speed function
    def showSpeed(self):
        print('The', self.__year, self.__model, '\'s current speed is', self.__speed,'miles per houer')

#create main function
def Main():
    year = ''
    make = ''
    #get car year and make
    make = input('What model is the car? \n')
    year = input('What year is the car?" \n')

    myCar = Car(year, make)

Main()

1 个答案:

答案 0 :(得分:0)

"构造函数" Python类必须被称为__init__,默认的__init__不接受参数,因此您会得到类型错误。 将您的init更改为:

def __init__(self, year, model):

无法重载__init__,但您可以使用带有默认值的命名参数,以便可以省略它们。

def __init__(self, year=None, model=None):

请参阅What is a clean, pythonic way to have multiple constructors in Python?