Python Classes and OOP Explained


To understand classes in Python, we should use parallelism. Let's suppose we are teachers and we are creating a course for students. 


Before we produce the course, we first make a list of all the types of curriculum items the course will have—for example, videoquizz, and coding exercise. Once we have a list of types, we can produce them by recording videos, writing quizzes, and building exercises. 


This is what we also do in Python. We first list the types/classes (i.e., import them). For example:

from fpdf FPDF # import the FPDF class


And then, we use the classes to produce instances:

mypdf = FPDF(orientation="portrait", format="letter")


However, listing some curriculum items is not enough when we create new innovative curriculum items. For example, this time, we will create Bug-Fixing Exercise and Programming Concept Blackboard types for the course. For these two, we need to create some accurate blueprints/classes first. The blueprints will be a description of what exactly these two types will consist of. 


The same goes for classes in Python. You create a blueprint/class and then use it to produce instances. For example, you create a Ticket class if you were creating a flight booking app:


class Ticket:

    self __init__(self, origin, destination, name):

        pass

    self.generate(self):

        pass


Then, you use that class to create multiple ticket instances:

ticket1 = Ticket("New York", "Bejing", "John Smith")

ticket2= Ticket("Anchorage", "Tokyo", "Jack Smith")

Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Subscribe to be the first to know when a new Python course is released.