Modifying Member Variables


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

# what 's wrong with my code? 
''' Traceback (most recent call last):
  File "python", line 16, in <module>
NameError: name 'drive_car' is not defined '''

Modifying Member Variables
condition = drive_car(self)

Remember that:

  1. drive_car is a method defined on an object, hence, you can only call it by doing my_car.drive_car()
  2. drive_car does not return anything, so your doing condition = drive_car(self) does not make sense.