I have two Classes Cars and CarCategory and in the CarCategory there is a list of Cars. The classes are defined as below:
class Car:
Name = None
def __init__(self, name):
self.Name = name
class CarCategory:
Category = None
lstCars = []
def __init__(self, name):
self.Cateogory = name
Then I defined a list of CarCategories as below:
lstCategories = []
tmpcat = CarCategory('SUV')
lstCategories.append(tmpcat)
tmpcat = CarCategory('MPV')
lstCategories.append(tmpcat)
Now, I am supposed to add list if cars into these categories, and I do it as bellow:
tmpCar = Car('X3')
lstCategories[0].lstCars.append(tmpCar)
The problem is that while doing this, the tmpCar is added to list of cars in both categories, i.e. it is added to both lstCategories[0] and lstCategories[1].
I am using, Spyeder and Python 3.7.
PS: I know this might not be the best way, and it is better to have Category property in Car class, but I need to do it like this.