classVehicle: color ='red'# This should not be changed engine ='v6'# Should be static and should not be changeddef__init__(self,body_type,make): self.vehicle_body = body_type self.vehicle_make = make
!!! BAD PRACTICE TO CHANGE THE CLASS ATTRIBUTES !!!
# To change the color from the Class above, which you should not do:Vehicle.color ='green'
This becomes a design problem as there are thousands of lines of code in a program.
!!! THIS IS ALSO A BAD PRACTICE !!! :
Instance Variable
car1 =Vehicle('suv', 'toyota')# By changing the color for the car1 instance# This changes the color just for the car1 instancecar1.color ='purple'# The class does not have a engine variable but by assigining it, you are creating an instance variable called v6 for car1. # The rest of the cars do not have an engine attributecar1.engine ='v6'
This also becomes a design issue as other developers can assign car instances willy nilly making this an issue
Makes interpreting the code hard
Overview Recap:
classVehicle:# The varialbe should not be changed outside of the class specification# This is a class attribute vehicle_counter =0# This method will always rundef__init__(self,body_type,make): self.vehicle_body = body_type self.vehicle_make = make# Returning a class attribute should be associated with the class# Should not be associated with the instance (self)defget_vehicle_count(self):return Vehicle.vehicle_counter# This increments the Class value and is not instance related Vehicle.vehicle_counter +=1