Python super()最多接受2个参数(给定3个)

Python super()最多接受2个参数(给定3个),python,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

上面的代码抛出

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()
第27行是

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

问题在于
super
调用的参数顺序。但我已在下面修复了代码中的其他问题:

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

问题在于
super
调用的参数顺序。但我已在下面修复了代码中的其他问题:

my_tesla=ElectricCar('tesla','benz','2016')
您使用的
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()
请注意,
对象
不属于此处的任何参数

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

super(ElectricCar, self).__init__(model, make, year)
您使用的
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()
请注意,
对象
不属于此处的任何参数

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

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

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

所以你的代码

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

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

请注意,参数顺序也不同。

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

所以你的代码

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

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

请注意,\uuu init\uuu()参数顺序也不同。

类电池
也应该继承自
对象
(当您使用时,将其更改为
电池
)。另外,
super
的第一个参数应该是类本身(不是实例),第二个参数应该是
self
。这是Python3还是Python2?@poke Python2根据
打印
@DeepSpace啊,是的,在那里,错过了那一个。
类电池
也应该继承自
对象
(当你使用时,将其更改为
电池
)。另外,
super
的第一个参数应该是类本身(不是实例),第二个参数应该是
self
。这是Python3还是Python2?@poke Python2根据
print
@DeepSpace判断是的,在那里,错过了那一个。