帮助,这是一个汽车经销商的测试,在那里,我将属性从Cars和Customers传递到CarDealership类。当我有很多变量时,我想练习使用**kwargs。
我正在实践多重继承,并使用**kwargs将潜在的多个参数传递给父类,这里的继承逻辑实际上是错误的,因为Customer不是CarDealership等等。
但是,我的问题是,为什么在尝试从Ferrari类将参数传递给父类时会出现错误。
下面是我的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类,但似乎做错了什么。
发布于 2018-03-02 01:20:58
使用super().__init__()而不是super.__init__()
https://stackoverflow.com/questions/49061295
复制相似问题