Define a property that must have the same value for every class instance (object)
Define a class attribute”color” with a default value white. I.e., Every Vehicle should be white.
Use the following code for this exercise.
Expected Output:
Color: White, Vehicle name: School Volvo, Speed: 180, Mileage: 12
Color: White, Vehicle name: Audi Q5, Speed: 240, Mileage: 18
Hint:
Define a
color
as a class variable in a Vehicle classSolution:
Variables created in
.__init__()
are called instance variables. An instance variable’s value is specific to a particular instance of the class. For example, in the solution, All Vehicle objects have a name and a max_speed, but the name and max_speed variables’ values will vary depending on the Vehicle instance.On the other hand, the class variable is shared between all class instances. You can define a class attribute by assigning a value to a variable name outside of
need an explanation for this answer? contact us directly to get an explanation for this answer.__init__()
.