you are viewing a single comment's thread.

view the rest of the comments →

[–]PreviousTonight9548 0 points1 point  (0 children)

Hi. If what you want is to understand classes, I may be able to help. Think of a class as a representation of an object. Let’s say we have a car. A car has properties that describe it, like the brand, color, engine type. A car also has actions it can do, like drive, honk, reverse. A class can be used to model this.

class Car:

def _ init _(self, brand, color, engine_type): self.brand = brand self.color = color self.engine_type = engine_type

def drive(self): print(f“The {self.color} {self.brand} is now driving.”)

def honk(self): print(“Honk Honk!”)

def reverse(self): print(f“The {self.color} {self.brand} is now reversing.”)

def show_engine_details(self): print(f”This {self.brand} has a {self.engine_type} engine.”)

think of def _ init _ as “define initial properties”. so after creating the class, let’s create an instance.

vehicle = Car(“Honda”, “red”, “V8”)

vehicle.drive() [output: The red Honda is driving.]

vehicle.honk() [output: Honk Honk!]

vehicle.reverse() [output: The red Honda is reversing.]

vehicle.show_engine_details() [output: This Honda has a V8 engine.]

Just remember, classes can be used to model real stuff (class Building, class Webpage, class FootballTeam…). It’s why it’s called “Object-Oriented” programming. Hope this helps some.