

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1. CLASS A class is the blueprint. It defines what something is and what it can do. It doesn't create anything by itself — it's just the template.
class Car:
2. CONSTRUCTOR The special method init that runs automatically when you create an instance. It sets up the object.
def init(self, make, model, year):
3. PARAMETERS The inputs the constructor (or any method) accepts. self is always first — it refers to the object being created. The rest are values you pass in.
def init(self, make, model, year):
4. INSTANCE VARIABLES (attributes) Variables stored ON the object using self. — they persist as long as the object exists.
self.make = make self.model = model self.year = year
5. METHODS Functions that belong to the class. Always take self as the first parameter.
def get_make(self): # getter return self.make
def set_make(self, make): # setter self.make = make
def describe(self): # regular method return str(self.year) + " " + self.make + " " + self.model
6. INSTANCE An actual object created FROM the class blueprint. This is where things come to life.
my_car = Car("Toyota", "Camry", 2020 )
7. ACCESSING ATTRIBUTES AND METHODS Using dot notation on the instance:
python
print(my_car.make) # accesses attribute directly
print(my_car.get_make()) # accesses attribute via getter
print(my_car.describe()) # calls a method
my_car.set_make("Honda") # calls a setter to change a value
Putting it all together:
python
class Car: # 1. CLASS def init(self, make, model, year): # 2. CONSTRUCTOR
year self.make = make # 4. INSTANCE VARIABLES self.model = model self.year = year
def get_make(self): # 5. METHOD (getter) return self.make
def set_make(self, make): # 5. METHOD (setter) self.make = make
def describe(self): # 5. METHOD (regular) return str(self.year) + " " + self.make + " " + self.model
my_car = Car("Toyota", "Camry", 2020 ) # 6. INSTANCE
print(my_car.describe()) # 7. ACCESSING METHODS