父类变量传递给" kwargs"汽车经销商

时间:2018-03-02 01:15:21

标签: python multiple-inheritance kwargs

帮助,这是对汽车经销商的测试,我将CarsCustomers的属性传递到CarDealership类。当我有很多变量时,我想练习使用**kwargs

这是我的Python代码:

class CarDealership:    
    def __init__(self, customer_id=None,car_id=None,clean=True,sold=False):
        self.customer_id = customer_id
        self.car_id = car_id
        self.clean = clean
        self.sold = sold

    def new_car(self, sold = False, car_clean = True,**kwargs):
        for key,value in kwargs.items():
            setattr(self,key,value)
        self.car_id = car_id
        self.brand = brand
        self.color = color
        self.car_type = car_type
        self.price = price

    def customer_Add(self, **kwargs):
        for key,value in kwargs.items():
            setattr(self,key,value)
        self.customer_name = customer_name
        self.customer_id = customer_id

    def customer_purchase(self, customer_id, car_id, price):
        if sold:
            print("Car sold!")
        else:
            self.sold = True
        self.customer_id = customer_id
        self.car_id = car_id
        self.price = price 

    def clean(self, car_id):
        self.car_clean = True

    def __del__(self):
        print("Car Sold")


#cars
class Car(CarDealership):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self,key,value)
        self.car_id = car_id
        self.brand = brand
        self.color = color
        self.car_type = car_type
        self.car_clean = car_clean
        self.sold = sold
        self.price = price
        data={"car_id":car_id,
              "brand":brand,
              "color":color,
              "car_type":car_type,
              "price":price,
            }
        super().__init__(car_id)
        super().new_car(**data)

class Customer(CarDealership):
    def __init__(self, **kwards):
        for key, value in kwargs.items():
            setattr(self,key,value)
        self.customer_id = customer_id
        self.customer_name = customer_name
        data={"customer_id":customer_id,
              "customer_name":customer_name
              }

class Ferarri(Car):
    def __init__(self):
        data={"car_id":1,
              "brand":"Ferrari",
              "color":"red",
              "car_type":"Racing",
              "price":1000000
              }
        super.__init__(**data)

cars = [Ferarri()]

for car in cars:
    arguments=[car,car.color,car.brand,car.car_type,car.sold,car.price]

    string = "{}, color:{}, brand:{}, car_type:{}, sold:{}, price:{}".format(*arguments)
    print(string)

这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/stazc/Desktop/AI/Python/Apps/carDealershipTry02.py", line 80, in <module>
    cars = [Ferarri()]
  File "C:/Users/stazc/Desktop/AI/Python/Apps/carDealershipTry02.py", line 78, in __init__
    super.__init__(**data)
TypeError: descriptor '__init__' of 'super' object needs an argument

我正在传递Ferrari类到Car类的参数,但似乎我做错了。

1 个答案:

答案 0 :(得分:0)

使用super().__init__()而不是super.__init__()