Classes Attributes vs Object Attributes

Class Attributes

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
		
# 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:

Last updated