简体   繁体   中英

Passing multiple arguments with (**kwargs) wildcard to Parent Class in Multiple Inheritance - Python

Help, This is a test for a Car Dealership where I am passing on attributes from Cars and Customers into the CarDealership class. I want to practice using **kwargs for when I have a lot of variables.

I am practicing multiple-inheritance and using **kwargs to pass potentially multiple arguments to the parent class, the inheritance logic is actually wrong here since the Customer is not a CarDealership etc.

My issue though is why am I getting an error when trying to pass the arguments to the parent class from the Ferrari class.

Here is my Python code:

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)

And here is the error I am getting:

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

I am passing on the arguments from the Ferrari class to the Car class but it seems I am doing something wrong.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM