Classes and Objects in Python

#define the Vehicle class
class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    
    #class function named description
    #we define a variable named desc_str and use 
    #string formatting inside the string
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." 
        % (self.name, self.color, self.kind, self.value)
        return desc_str
 
#instantiate two objects of the Vehicle Class
car1 = Vehicle()
car2 = Vehicle()
 
#Modify variables for car1 object inherited from the Vehicle Class

car1.name = "Fer"
car1.kind = "convertible"
car1.color = "red"
car1.value = float(60000.00)

#Modify variables for car2 object inherited from the Vehicle Class

car2.name = "Jump"
car2.kind = "van"
car2.color = "blue"
car2.value = float(10000.00)

print(car1.description())
print(car2.description())

Leave a comment

Design a site like this with WordPress.com
Get started