class Vehicle:
color = 'red' # This should not be changed
engine = 'v6' # Should be static and should not be changed
def __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 instance
car1.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 attribute
car1.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:
class Vehicle:
# The varialbe should not be changed outside of the class specification
# This is a class attribute
vehicle_counter = 0
# This method will always run
def __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)
def get_vehicle_count(self):
return Vehicle.vehicle_counter
# This increments the Class value and is not instance related
Vehicle.vehicle_counter += 1