Why is there no change in the value of instance attribute even if I wrote my code to perform it (python)

40 Views Asked by At

In my code I delare a class "Car" and amongs other attributes also a private attribute called "__isOnSale". It refers to the cars that are currently on sale. Then I aim to provide my class Car with a possibility to update this attribute using class methods GetIsOnSale and SetIsOnSale. I should be able to modify __isOnSale for the cars that their brand belongs to brandOnSale, which is a variable I declare outside of the class "Car".

As a result, the isOnSale status of the car01 (which is Opel which belongs to the brandOnSale) should be modified, but it does not happen. Where is there a mistake in my code?

brandOnSale = "Opel"

class Car:

    numberOfCars = 0
    listOfCars = []

    def __init__(self, brand, model, isAirbagOK, isMechanicsOK, isOnSale):
        self.brand = brand
        self.model = model
        self.isAirbagOK = isAirbagOK
        self.isMechanicsOK = isMechanicsOK
        self.__isOnSale = isOnSale              # <------------------------
        Car.numberOfCars +=1
        Car.listOfCars.append(self)

    def isDamaged(self):
        return not (self.isAirbagOK and self.isMechanicsOK)

    def GetInfo(self):
        print("{} {}".format(self.brand,self.model).upper())
        print("Air Bag OK? -  {}".format(self.isAirbagOK))
        print("Mechanics OK?  - {}".format(self.isMechanicsOK))
        print("IS ON SALE? - {}".format(self.__isOnSale))  # <----------- 
        print("---------------------------")

    def GetIsOnSale(self):              # <----------- 
        return self.__isOnSale                      

    def SetIsOnSale(self, newIsOnSaleStatus):               
        if self.brand == brandOnSale:                        # <---------        
            self.__isOnSale == newIsOnSaleStatus
            print("Changing status isOnSale valid. Changing {} for {} ".format(newIsOnSaleStatus, self.brand))
        else:
            print("Cannot change status isOnSale for {}. Sale valid only for {}".format(self.brand,brandOnSale))


car01=Car('Opel','astra', True, True, False)
car02=Car('VW','touran',True, True, False)

cars = [car01, car02]

car01.GetInfo()   
car02.GetInfo()

print("Status of cars: car01: ",car01.GetIsOnSale(), "car02: ",car02.GetIsOnSale()) 

car01.SetIsOnSale(True)   #< ----------------      
car02.SetIsOnSale(True)        

print("Status of cars: car01: ",car01.GetIsOnSale(), "car02: ",car02.GetIsOnSale())  #< ---------------- 

Please point out my mistake. Thank you very much in advance.

2

There are 2 best solutions below

0
nsw42 On

It's a simple typo - nothing complicated going on. You've got a double equals (ie. test for equality) in your assignment to __isOnSale in SetIsOnSale. Change that to a single equals and you'll be sorted.

0
DG. On

In SetIsOnSale you are not actually assigning __isOnSale.

self.__isOnSale == newIsOnSaleStatus # Evaluates to False

Remove one = sign and you're good.

self.__isOnSale = newIsOnSaleStatus