Modifying member variables - Explanation requested


#1



Hello, dear all. Could somebody be so kind and explain me more explicitly, what does this code mean

print my_car.condition
my_car.drive_car()
print my_car.condition


I took it from my correctly solved exercise.

I just had an intuitive feeling about the solution in that specific part rather than fool understanding what is going on. Thank you, guys =)


class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        print ("This is a %s %s with %s MPG.") % (self.color, self.model, str(self.mpg))
    
    def drive_car(self):
        self.condition = "used"
        
my_car = Car("DeLorean", "silver", 88)

print my_car.condition
my_car.drive_car()
print my_car.condition


#2

well, first you print the condition (which is a member/class variable)

then you take the car for a drive (calling drive_car method, which changes the value of class variable)

then you print the condition of the car again, its now used given you drove in it


#3

Thank you, @stetim94.
Correct me please, if I am wrong.
So generally speaking I am taking the first command print my_car.condition and it produces the condition which is "new" which was announced at the beginning of the code.
Afterwards, I am updating my 'condition' with a method my_car.drive_car() and later on it prints new condition of my car print my_car.condition? Thank you


#4

nothing to correct, you are right.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.