super()最多需要2个参数(给定3个)

时间:2017-05-22 06:49:19

标签: python

class car(object):
    """A simple attempt to represent a car."""
    def __init__(self,make,model,year):
        self.make=make
        self.model=model
        self.year=year
    def get_descriptive_name(self):
        """getting full name of the car"""
        longname=str(self.year)+" "+self.make+" "+self.model
        return longname

class battery():
    """defining the new battery class"""
    def __init__(self,battery_size=70):
        self.battery_size=battery_size
    def describe_battery(self):
        """Print a statement describing the battery size."""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")
    def get_range(self):
        if self.battery_size==70:
            range=210
        elif self.battery_size==90:
            range=270
        message = "This car can go approximately " + str(range)
        message += " miles on a full charge."
        print(message)

class ElectricCar(car):
    def __init__(self,make,model,year):
        super(object,ElectricCar).__init__(model,make,year)
        self.battery=Battery

my_tesla=ElectricCar('tesla','benz','2016')
print my_tesla.get_descriptive_name
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

以上代码抛出

Traceback (most recent call last):
  File "python", line 27, in <module>
  File "python", line 25, in __init__
TypeError: super() takes at most 2 arguments (3 given)

上面的代码会抛出错误,显示我发布的上述错误。因为它抛出super()参数错误。如何解决这个错误。其中第25行是:

super(object,ElectricCar).__init__(model,make,year)

和第27行是

my_tesla=ElectricCar('tesla','benz','2016')

3 个答案:

答案 0 :(得分:1)

问题在于super调用的参数顺序。但是我的代码还有其他问题,我已在下面修复过:

#code throws super argument error
class Car(object): # Class name should be upper case
    """A simple attempt to represent a car."""
    def __init__(self,make,model,year):
        self.make=make
        self.model=model
        self.year=year
    def get_descriptive_name(self):
        """getting full name of the car"""
        longname=str(self.year)+" "+self.make+" "+self.model
        return longname

class Battery(object): # Upper case
    """defining the new battery class"""

    def __init__(self, battery_size=70):
        self.battery_size=battery_size

    def describe_battery(self):
        """Print a statement describing the battery size."""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

    def get_range(self):
        if self.battery_size==70:
           range=210
        elif self.battery_size==90:
           range=270
        message = "This car can go approximately " + str(range)
        message += " miles on a full charge."
        print(message)   

class ElectricCar(Car):

    def __init__(self,make,model,year):
        super(ElectricCar, self).__init__(make,model,year) # Fix super call and init order of params
        self.battery=Battery() # Upper case and missing ()


my_tesla=ElectricCar('tesla','benz','2016')
print my_tesla.get_descriptive_name
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

答案 1 :(得分:0)

您错误地使用了super()。第一个参数应该是当前类型,第二个参数应该是当前实例。所以它应该是这样的:

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

请注意,此处object不属于任何参数。

修复后,您将收到一个新错误,即Battery不存在。为了解决这个问题,您需要将其更改为battery,并且您还需要调用它来实际创建新的battery对象:

class ElectricCar(car):
    def __init__(self, make, model, year):
        super(ElectricCar, self).__init__(model, make, year)
        self.battery = battery()

答案 2 :(得分:0)

在调用super()时你犯了错误。 super()接受两个参数,第一个应该是类型,另一个应该是实例。

所以你的代码

class ElectricCar(car):
    def __init__(self,make,model,year):
        super(object,ElectricCar).__init__(model,make,year)
        self.battery=Battery

应该是

class ElectricCar(car):
    def __init__(self,make,model,year):
        super(ElectricCar,self).__init__(make,model,year)
        self.battery=Battery

请注意 __ init __()参数顺序也不同。

相关问题